import firebase from 'firebase/app' ; import 'firebase/auth' ; import * as firebaseui from 'firebaseui' ; import { Preconditions } from "polar-shared/src/Preconditions" ; import './FirebaseUIAuth.css' ; import 'firebaseui/dist/firebaseui.css' ; import { Analytics } from "../analytics/Analytics" ; const SIGN_IN_SUCCESS_URL = 'http://localhost:8005/' ; const TOS_URL = 'https://getpolarized.io/terms-of-service.html' ; const PRIVACY_POLICY_URL = 'https://getpolarized.io/privacy-policy.html' ; export namespace FirebaseUIAuth { param opts export function login ( opts = { } ) { console . log ( "Triggering Firebase UI auth: " , opts ) ; const auth = firebase . auth ( ) ; Preconditions . assertPresent ( firebaseui , 'firebaseui' ) ; Preconditions . assertPresent ( firebaseui . auth , 'firebaseui.auth' ) ; const containerSelector = opts . containerSelector || '#firebaseui-auth-container' ; function computeSignInOptions ( ) { if ( opts . provider ) { console . log ( "Authenticating with provider: " + opts . provider ) ; return [ { provider : opts . provider } ] ; } return [ { provider : firebase . auth . GoogleAuthProvider . PROVIDER_ID , customParameters : { prompt : 'select_account' } } , firebase . auth . EmailAuthProvider . PROVIDER_ID , ] ; } const signInOptions = computeSignInOptions ( ) ; const uiConfig = { signInFlow : opts . signInFlow || 'redirect' , callbacks : { signInSuccessWithAuthResult : ( authResult , redirectUrl ? ) => { if ( authResult . additionalUserInfo ?. isNewUser ) { console . log ( "New user authenticated" ) ; Analytics . event2 ( 'new-user-signup' ) ; document . location . href = '/#welcome' ; return false ; } if ( redirectUrl ) { console . log ( "Sending to redirect URL: " + redirectUrl ) ; document . location . href = redirectUrl ; return false ; } return true ; } } , queryParameterForWidgetMode : 'mode' , signInSuccessUrl : opts . signInSuccessUrl || SIGN_IN_SUCCESS_URL , signInOptions , tosUrl : TOS_URL , privacyPolicyUrl : ( ) => { window . location . assign ( PRIVACY_POLICY_URL ) ; } } ; const ui = new firebaseui . auth . AuthUI ( auth ) ; ui . start ( containerSelector , uiConfig ) ; } export async function loginWithCustomToken ( customToken ) { Preconditions . assertPresent ( firebaseui , 'firebaseui' ) ; Preconditions . assertPresent ( firebaseui . auth , 'firebaseui.auth' ) ; const auth = firebase . auth ( ) ; const userCredential = await auth . signInWithCustomToken ( customToken ) ; } interface IProviderAuthOpts { readonly signInSuccessUrl ? : string ; } function signInSuccessWithAuthResult ( authResult , redirectUrl ? ) { if ( authResult . additionalUserInfo ?. isNewUser ) { console . log ( "New user authenticated" ) ; Analytics . event2 ( 'new-user-signup' ) ; document . location . href = '/#welcome' ; return false ; } if ( redirectUrl ) { console . log ( "Sending to redirect URL: " + redirectUrl ) ; document . location . href = redirectUrl ; return false ; } return true ; } function authWithProvider ( provider , opts = { } ) { const auth = firebase . auth ( ) ; const uiConfig = { signInFlow : 'redirect' , callbacks : { signInSuccessWithAuthResult } , queryParameterForWidgetMode : 'mode' , signInSuccessUrl : opts . signInSuccessUrl || SIGN_IN_SUCCESS_URL , signInOptions : [ provider ] , tosUrl : TOS_URL , privacyPolicyUrl : ( ) => { window . location . assign ( PRIVACY_POLICY_URL ) ; } , immediateFederatedRedirect : true } ; const ui = new firebaseui . auth . AuthUI ( auth ) ; ui . start ( '#firebaseui-auth-container' , uiConfig ) ; } export function authWithGoogle ( opts = { } ) { const provider = { provider : firebase . auth . GoogleAuthProvider . PROVIDER_ID , customParameters : { prompt : 'select_account' } } ; authWithProvider ( provider , opts ) ; } export function authWithEmailLink ( opts = { } ) { const provider = { provider : firebase . auth . EmailAuthProvider . PROVIDER_ID , signInMethod : firebase . auth . EmailAuthProvider . EMAIL_LINK_SIGN_IN_METHOD , } ; authWithProvider ( provider , opts ) ; } } export interface FirebaseUIAuthOptions { readonly containerSelector ? : string ; readonly signInSuccessUrl ? : string ; /**
     * The sign in flow type.  Either popup or redirect. Default is redirect.
     */ readonly signInFlow ? : 'popup' | 'redirect' ; /**
     * When provided we use a custom SAML provider with the given name.
     */ readonly provider ? : string ; }