import { User, getAuth, signInWithCredential, initializeAuth, OAuthCredential } from "firebase/auth" import { FirebaseApp } from "firebase/app" // ref https://firebase.google.com/docs/web/setup#access-firebase. /** * Sign in w/ OAuth 2.0 token. * @param firebaseApp - the configured instance of the Firebase App in use. * @param credentials - the OAuth credential generated from token exchange. */ export const signInToFirebaseWithCredentials = async (firebaseApp: FirebaseApp, credentials: OAuthCredential) => signInWithCredential(initializeAuth(firebaseApp), credentials) /** * Return the current authenticated user in the given Firebase Application. * @param firebaseApp - the configured instance of the Firebase App in use. * @returns - the object containing the data about the current authenticated user in the given Firebase application. */ export const getCurrentFirebaseAuthUser = (firebaseApp: FirebaseApp): User => { const user = getAuth(firebaseApp).currentUser if (!user) throw new Error( `Unable to find the user currently authenticated with Firebase. Verify that the Firebase application is properly configured and repeat user authentication before trying again.` ) return user } /** * Check if the user can claim to be a coordinator. * @param user - the user to be checked. * @returns Promise - true if the user is a coordinator, false otherwise. */ export const isCoordinator = async (user: User) => { const userTokenAndClaims = await user.getIdTokenResult() return !!userTokenAndClaims.claims.coordinator }