|
| 1 | +import React, { useCallback, useMemo } from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + AppBar, |
| 5 | + Authenticator, |
| 6 | + CircularProgressCenter, |
| 7 | + Drawer, |
| 8 | + FireCMS, |
| 9 | + ModeControllerProvider, |
| 10 | + NavigationRoutes, |
| 11 | + Scaffold, |
| 12 | + SideDialogs, |
| 13 | + SnackbarProvider, |
| 14 | + useBuildLocalConfigurationPersistence, |
| 15 | + useBuildModeController, |
| 16 | + useBuildNavigationController, |
| 17 | + useValidateAuthenticator |
| 18 | +} from "@firecms/core"; |
| 19 | +import { |
| 20 | + FirebaseAuthController, |
| 21 | + FirebaseLoginView, |
| 22 | + FirebaseSignInProvider, |
| 23 | + FirebaseUserWrapper, |
| 24 | + useFirebaseAuthController, |
| 25 | + useFirebaseStorageSource, |
| 26 | + useFirestoreDelegate, |
| 27 | + useInitialiseFirebase, |
| 28 | +} from "@firecms/firebase"; |
| 29 | +import { CenteredView } from "@firecms/ui"; |
| 30 | + |
| 31 | +import { firebaseConfig } from "./firebase_config"; |
| 32 | +import {chatbotsCollection} from "./collections/chatbots"; |
| 33 | + |
| 34 | +export function App() { |
| 35 | + |
| 36 | + // Use your own authentication logic here |
| 37 | + const myAuthenticator: Authenticator<FirebaseUserWrapper> = useCallback(async ({ |
| 38 | + user, |
| 39 | + authController |
| 40 | + }) => { |
| 41 | + |
| 42 | + if (user?.email?.includes("flanders")) { |
| 43 | + // You can throw an error to prevent access |
| 44 | + throw Error("Stupid Flanders!"); |
| 45 | + } |
| 46 | + |
| 47 | + const idTokenResult = await user?.firebaseUser?.getIdTokenResult(); |
| 48 | + const userIsAdmin = idTokenResult?.claims.admin || user?.email?.endsWith("@firecms.co"); |
| 49 | + |
| 50 | + console.log("Allowing access to", user); |
| 51 | + |
| 52 | + // we allow access to every user in this case |
| 53 | + return true; |
| 54 | + }, []); |
| 55 | + |
| 56 | + const collections = useMemo(() => [ |
| 57 | + chatbotsCollection, |
| 58 | + ], []); |
| 59 | + |
| 60 | + const { |
| 61 | + firebaseApp, |
| 62 | + firebaseConfigLoading, |
| 63 | + configError |
| 64 | + } = useInitialiseFirebase({ |
| 65 | + firebaseConfig |
| 66 | + }); |
| 67 | + |
| 68 | + // Controller used to manage the dark or light color mode |
| 69 | + const modeController = useBuildModeController(); |
| 70 | + |
| 71 | + const signInOptions: FirebaseSignInProvider[] = ["google.com", "password"]; |
| 72 | + |
| 73 | + // Controller for managing authentication |
| 74 | + const authController: FirebaseAuthController = useFirebaseAuthController({ |
| 75 | + firebaseApp, |
| 76 | + signInOptions |
| 77 | + }); |
| 78 | + |
| 79 | + // Controller for saving some user preferences locally. |
| 80 | + const userConfigPersistence = useBuildLocalConfigurationPersistence(); |
| 81 | + |
| 82 | + // Delegate used for fetching and saving data in Firestore |
| 83 | + const firestoreDelegate = useFirestoreDelegate({ |
| 84 | + firebaseApp |
| 85 | + }); |
| 86 | + |
| 87 | + // Controller used for saving and fetching files in storage |
| 88 | + const storageSource = useFirebaseStorageSource({ |
| 89 | + firebaseApp |
| 90 | + }); |
| 91 | + |
| 92 | + const { |
| 93 | + authLoading, |
| 94 | + canAccessMainView, |
| 95 | + notAllowedError |
| 96 | + } = useValidateAuthenticator({ |
| 97 | + authController, |
| 98 | + authenticator: myAuthenticator, |
| 99 | + dataSourceDelegate: firestoreDelegate, |
| 100 | + storageSource |
| 101 | + }); |
| 102 | + |
| 103 | + const navigationController = useBuildNavigationController({ |
| 104 | + collections, |
| 105 | + authController, |
| 106 | + dataSourceDelegate: firestoreDelegate |
| 107 | + }); |
| 108 | + |
| 109 | + if (firebaseConfigLoading || !firebaseApp) { |
| 110 | + return <> |
| 111 | + <CircularProgressCenter/> |
| 112 | + </>; |
| 113 | + } |
| 114 | + |
| 115 | + if (configError) { |
| 116 | + return <CenteredView>{configError}</CenteredView>; |
| 117 | + } |
| 118 | + |
| 119 | + return ( |
| 120 | + <SnackbarProvider> |
| 121 | + <ModeControllerProvider value={modeController}> |
| 122 | + <FireCMS |
| 123 | + navigationController={navigationController} |
| 124 | + authController={authController} |
| 125 | + userConfigPersistence={userConfigPersistence} |
| 126 | + dataSourceDelegate={firestoreDelegate} |
| 127 | + storageSource={storageSource} |
| 128 | + > |
| 129 | + {({ |
| 130 | + context, |
| 131 | + loading |
| 132 | + }) => { |
| 133 | + |
| 134 | + if (loading || authLoading) { |
| 135 | + return <CircularProgressCenter size={"large"}/>; |
| 136 | + } |
| 137 | + |
| 138 | + if (!canAccessMainView) { |
| 139 | + return <FirebaseLoginView authController={authController} |
| 140 | + firebaseApp={firebaseApp} |
| 141 | + signInOptions={signInOptions} |
| 142 | + notAllowedError={notAllowedError}/>; |
| 143 | + } |
| 144 | + |
| 145 | + return <Scaffold |
| 146 | + autoOpenDrawer={false}> |
| 147 | + <AppBar title={"My demo app"}/> |
| 148 | + <Drawer/> |
| 149 | + <NavigationRoutes/> |
| 150 | + <SideDialogs/> |
| 151 | + </Scaffold>; |
| 152 | + }} |
| 153 | + </FireCMS> |
| 154 | + </ModeControllerProvider> |
| 155 | + </SnackbarProvider> |
| 156 | + ); |
| 157 | + |
| 158 | +} |
| 159 | + |
| 160 | +export default App; |
0 commit comments