1717
1818import { FirebaseApp , getApp , _getProvider } from '@firebase/app' ;
1919
20- import { initializeAuth } from '..' ;
20+ import {
21+ initializeAuth ,
22+ beforeAuthStateChanged ,
23+ onIdTokenChanged ,
24+ connectAuthEmulator
25+ } from '..' ;
2126import { registerAuth } from '../core/auth/register' ;
2227import { ClientPlatform } from '../core/util/version' ;
2328import { browserLocalPersistence } from './persistence/local_storage' ;
2429import { browserSessionPersistence } from './persistence/session_storage' ;
2530import { indexedDBLocalPersistence } from './persistence/indexed_db' ;
2631import { browserPopupRedirectResolver } from './popup_redirect' ;
27- import { Auth } from '../model/public_types' ;
32+ import { Auth , User } from '../model/public_types' ;
33+ import { getDefaultEmulatorHost , getExperimentalSetting } from '@firebase/util' ;
34+
35+ const DEFAULT_ID_TOKEN_MAX_AGE = 5 * 60 ;
36+ const authIdTokenMaxAge =
37+ getExperimentalSetting ( 'authIdTokenMaxAge' ) || DEFAULT_ID_TOKEN_MAX_AGE ;
38+
39+ let lastPostedIdToken : string | undefined | null = null ;
40+
41+ const mintCookieFactory = ( url : string ) => async ( user : User | null ) => {
42+ const idTokenResult = user && ( await user . getIdTokenResult ( ) ) ;
43+ const idTokenAge =
44+ idTokenResult &&
45+ ( new Date ( ) . getTime ( ) - Date . parse ( idTokenResult . issuedAtTime ) ) / 1_000 ;
46+ if ( idTokenAge && idTokenAge > authIdTokenMaxAge ) {
47+ return ;
48+ }
49+ // Specifically trip null => undefined when logged out, to delete any existing cookie
50+ const idToken = idTokenResult ?. token ;
51+ if ( lastPostedIdToken === idToken ) {
52+ return ;
53+ }
54+ lastPostedIdToken = idToken ;
55+ await fetch ( url , {
56+ method : idToken ? 'POST' : 'DELETE' ,
57+ headers : idToken
58+ ? {
59+ 'Authorization' : `Bearer ${ idToken } `
60+ }
61+ : { }
62+ } ) ;
63+ } ;
2864
2965/**
3066 * Returns the Auth instance associated with the provided {@link @firebase/app#FirebaseApp }.
@@ -41,14 +77,30 @@ export function getAuth(app: FirebaseApp = getApp()): Auth {
4177 return provider . getImmediate ( ) ;
4278 }
4379
44- return initializeAuth ( app , {
80+ const auth = initializeAuth ( app , {
4581 popupRedirectResolver : browserPopupRedirectResolver ,
4682 persistence : [
4783 indexedDBLocalPersistence ,
4884 browserLocalPersistence ,
4985 browserSessionPersistence
5086 ]
5187 } ) ;
88+
89+ const authTokenSyncUrl = getExperimentalSetting ( 'authTokenSyncURL' ) ;
90+ if ( authTokenSyncUrl ) {
91+ const mintCookie = mintCookieFactory ( authTokenSyncUrl ) ;
92+ beforeAuthStateChanged ( auth , mintCookie , ( ) =>
93+ mintCookie ( auth . currentUser )
94+ ) ;
95+ onIdTokenChanged ( auth , user => mintCookie ( user ) ) ;
96+ }
97+
98+ const authEmulatorHost = getDefaultEmulatorHost ( 'auth' ) ;
99+ if ( authEmulatorHost ) {
100+ connectAuthEmulator ( auth , `http://${ authEmulatorHost } ` ) ;
101+ }
102+
103+ return auth ;
52104}
53105
54106registerAuth ( ClientPlatform . BROWSER ) ;
0 commit comments