import { FirebaseError } from 'firebase/app' ; import { AuthError , fetchSignInMethodsForEmail , getAuth , linkWithCredential , OAuthCredential , onAuthStateChanged , signInWithPopup , signOut as firebaseSignOut , } from 'firebase/auth' ; import { store } from '..' ; import { firebaseApp } from '../../firebase' ; import { FirebaseUser } from '../../models/user' ; import { getFederatedProvider , getFederatedProviderClass , PROVIDER } from '../../utils/providers' ; import { FeaturedSessionsActions , REMOVE_USER_FEATURED_SESSIONS } from '../featured-sessions/types' ; import { FeedbackActions , WIPE_PREVIOUS_FEEDBACK } from '../feedback/types' ; import { getToken } from '../notifications/actions' ; import { resetSubscribed } from '../subscribe/actions' ; import { removeUser , setUser } from '../user/actions' ; import { AUTH , AuthActions , AUTH_FAILURE , AUTH_SUCCESS , ExistingAccountError , UN_AUTH , } from './types' ; const auth = getAuth ( firebaseApp ) ; export const signIn = async ( providerId ) => { store . dispatch < AuthActions > ( { type : AUTH } ) ; const provider = getFederatedProvider ( providerId ) ; try { await signInWithPopup ( auth , provider ) ; store . dispatch < AuthActions > ( { type : AUTH_SUCCESS , payload : true } ) ; } catch ( error ) { if ( error instanceof FirebaseError ) { setAuthFailure ( error as AuthError , providerId ) ; } else { throw error ; } } } ; const setAuthFailure = async ( error , credentialProviderId ) => { const credential = getFederatedProviderClass ( credentialProviderId ) . credentialFromError ( error ) ; const [ providerId ] = await fetchSignInMethodsForEmail ( auth , error . customData . email ! ) ; const payload = { code : error . code , credential , email : error . customData . email , providerId : providerId as PROVIDER , } ; store . dispatch < AuthActions > ( { type : AUTH_FAILURE , payload } ) ; } ; export const mergeAccounts = async ( providerId , pendingCredential ) => { const provider = getFederatedProvider ( providerId ) ; try { const { user } = await signInWithPopup ( auth , provider ) ; await linkWithCredential ( user , pendingCredential ) ; store . dispatch < AuthActions > ( { type : AUTH_SUCCESS , payload : true } ) ; } catch ( error ) { if ( error instanceof FirebaseError ) { setAuthFailure ( error as AuthError , providerId ) ; } else { throw error ; } } } ; export const onUser = ( ) => { onAuthStateChanged ( auth , ( user ) => { if ( user ) { setUser ( user ) ; getToken ( true ) ; } else { unAuth ( ) ; removeUser ( ) ; resetSubscribed ( ) ; store . dispatch < FeaturedSessionsActions > ( { type : REMOVE_USER_FEATURED_SESSIONS } ) ; store . dispatch < FeedbackActions > ( { type : WIPE_PREVIOUS_FEEDBACK } ) ; } } ) ; } ; const unAuth = ( ) => store . dispatch < AuthActions > ( { type : UN_AUTH } ) ; export const signOut = ( ) => firebaseSignOut ( auth ) ;