import { useEffect, useState } from "react"; import { useSnackbarController } from "@firecms/core"; import { BuildIcon, Button, LoadingButton, Typography } from "@firecms/ui"; import { FireCMSBackend } from "../types"; import { useFireCMSBackend } from "../hooks"; import { useNavigate } from "react-router-dom"; export type CloudError = { code?: string, message: string, projectId?: string, data?: { missingPermissions?: string[], errorDetails?: object } }; export function CloudErrorView({ error, fireCMSBackend, onFixed, onRetry, allowServiceAccountBypass = false }: { error: CloudError, fireCMSBackend?: FireCMSBackend, onFixed?: () => void, onRetry?: () => void, allowServiceAccountBypass?: boolean; }) { const navigate = useNavigate(); const { code, message, projectId } = error; if (code === "service-account-missing" && projectId && fireCMSBackend) { return ; } else if (code === "service-account-corrupt" && projectId && fireCMSBackend) { return ; } else if (code === "user-has-to-accept-googles-terms-of-service" && projectId) { return ; } else if (code === "user-has-no-previous-firebase-projects" && projectId) { return ; } else if (code === "firecms-user-not-found") { return
The user trying to log in is not registered in the client project. Make sure the user exists in the client project and try again. If the problem persists, reach us at hello@firecms.co , or in our Discord channel.
; } else if ((code === "service-account-missing-permissions" || code === "user-missing-permissions") && allowServiceAccountBypass) { return (
Missing Permissions {error.data?.missingPermissions && <> Your Google Cloud account is missing the following permissions on this project: {error.data.missingPermissions.map((permission) =>
  • {permission}
  • )}
    } 💡 Recovery Option: You can bypass Google OAuth permissions entirely by using a Firebase Service Account JSON key.
    {onRetry && ( )}
    ); } else if (code === "service-account-missing-permissions" || code === "user-missing-permissions") { return ; } const isPermissionError = (code === "permission-denied" || code === "permission_denied" || (message && message.toLowerCase().includes("permission")) || (message && message.toLowerCase().includes("caller does not have"))) && allowServiceAccountBypass; if (isPermissionError) { return (
    Google Cloud Permission Error {message} This error usually occurs because your Google Cloud account does not have sufficient permissions (like Owner or IAM Admin roles) to enable services or automatically generate a service account. 💡 Recovery Option: You can bypass Google OAuth permissions entirely by using a Firebase Service Account JSON key.
    {onRetry && ( )}
    ); } return (
    {message} {error.data && Object.keys(error.data).length > 0 &&
                    {JSON.stringify(error.data, null, 2)}
                
    } {onRetry && }
    ); } function CloudMissingServiceAccountErrorView({ fireCMSBackend, projectId, onFixed, message }: { fireCMSBackend: FireCMSBackend, projectId: string, onFixed?: () => void, message: string }) { const { projectsApi } = useFireCMSBackend(); const snackbarController = useSnackbarController(); const [isSubmitting, setIsSubmitting] = useState(false); const [pendingLogin, setPendingLogin] = useState(false); const doCreateServiceAccount = async () => { if (!fireCMSBackend.googleCredential?.accessToken) { throw new Error("SassMissingServiceAccountErrorView: No access token found"); } setIsSubmitting(true); return projectsApi.createServiceAccount(fireCMSBackend.googleCredential.accessToken, projectId, true) .finally(() => setIsSubmitting(false)) }; useEffect(() => { if (pendingLogin && fireCMSBackend.googleCredential?.accessToken) { doCreateServiceAccount().then(() => { snackbarController.open({ type: "success", message: "Service account created successfully" }); if (onFixed) onFixed(); }).catch(e => { snackbarController.open({ type: "error", message: "Service account creation error: " + e.message }); }); setPendingLogin(false); } }, [pendingLogin, fireCMSBackend.googleCredential?.accessToken]); const onClick = async () => { const accessToken = fireCMSBackend.googleCredential?.accessToken; if (!accessToken) { setPendingLogin(true); fireCMSBackend.googleLogin(true); } else { await doCreateServiceAccount(); } }; return
    {message} } > Fix
    } function CloudNeedsToAcceptTermsErrorView({ projectId, onFixed }: { projectId: string, onFixed?: () => void, }) { return
    You need to accept Google's terms of service before you can use this service. You can do so by visiting the following link: {`https://console.cloud.google.com/welcome?project=${projectId}`}
    } function CloudNoPreviousFirebaseProjectsErrorView({ projectId, onFixed }: { projectId: string, onFixed?: () => void, }) { return
    You need to accept Firebase's terms of service before you can use this service. You can do so by visiting the following link: {"https://console.firebase.google.com/"}
    } function ServiceAccountMissingPermissions(props: { missingPermissions: string[] | undefined, }) { return
    Missing permissions {props.missingPermissions && <> This service account is missing the following permissions: {props.missingPermissions.map((permission) =>
  • {permission}
  • )}
    }
    ; }