@@ -30,7 +30,7 @@ import url = require('url');
3030import * as mocks from '../resources/mocks' ;
3131import { AuthProviderConfig } from '../../src/auth/auth-config' ;
3232import { deepExtend , deepCopy } from '../../src/utils/deep-copy' ;
33- import { User } from '@firebase/auth-types' ;
33+ import { User , FirebaseAuth } from '@firebase/auth-types' ;
3434
3535/* tslint:disable:no-var-requires */
3636const chalk = require ( 'chalk' ) ;
@@ -91,6 +91,10 @@ function randomOidcProviderId(): string {
9191 return 'oidc.' + generateRandomString ( 10 , false ) . toLowerCase ( ) ;
9292}
9393
94+ function clientAuth ( ) : FirebaseAuth {
95+ expect ( firebase . auth ) . to . be . ok ;
96+ return firebase . auth ! ( ) ;
97+ }
9498
9599describe ( 'admin.auth' , ( ) => {
96100
@@ -213,7 +217,7 @@ describe('admin.auth', () => {
213217 let currentIdToken : string ;
214218 let currentUser : User ;
215219 // Sign in with an email and password account.
216- return firebase . auth ! ( ) . signInWithEmailAndPassword ( mockUserData . email , mockUserData . password )
220+ return clientAuth ( ) . signInWithEmailAndPassword ( mockUserData . email , mockUserData . password )
217221 . then ( ( { user} ) => {
218222 expect ( user ) . to . exist ;
219223 currentUser = user ! ;
@@ -248,7 +252,7 @@ describe('admin.auth', () => {
248252 } )
249253 . then ( ( ) => {
250254 // New sign-in should succeed.
251- return firebase . auth ! ( ) . signInWithEmailAndPassword (
255+ return clientAuth ( ) . signInWithEmailAndPassword (
252256 mockUserData . email , mockUserData . password ) ;
253257 } )
254258 . then ( ( { user} ) => {
@@ -273,7 +277,7 @@ describe('admin.auth', () => {
273277 // Confirm custom claims set on the UserRecord.
274278 expect ( userRecord . customClaims ) . to . deep . equal ( customClaims ) ;
275279 expect ( userRecord . email ) . to . exist ;
276- return firebase . auth ! ( ) . signInWithEmailAndPassword (
280+ return clientAuth ( ) . signInWithEmailAndPassword (
277281 userRecord . email ! , mockUserData . password ) ;
278282 } )
279283 . then ( ( { user} ) => {
@@ -302,8 +306,8 @@ describe('admin.auth', () => {
302306 // Custom claims should be cleared.
303307 expect ( userRecord . customClaims ) . to . deep . equal ( { } ) ;
304308 // Force token refresh. All claims should be cleared.
305- expect ( firebase . auth ! ( ) . currentUser ) . to . exist ;
306- return firebase . auth ! ( ) . currentUser ! . getIdToken ( true ) ;
309+ expect ( clientAuth ( ) . currentUser ) . to . exist ;
310+ return clientAuth ( ) . currentUser ! . getIdToken ( true ) ;
307311 } )
308312 . then ( ( idToken ) => {
309313 // Verify ID token contents.
@@ -368,7 +372,7 @@ describe('admin.auth', () => {
368372 isAdmin : true ,
369373 } )
370374 . then ( ( customToken ) => {
371- return firebase . auth ! ( ) . signInWithCustomToken ( customToken ) ;
375+ return clientAuth ( ) . signInWithCustomToken ( customToken ) ;
372376 } )
373377 . then ( ( { user} ) => {
374378 expect ( user ) . to . exist ;
@@ -388,7 +392,7 @@ describe('admin.auth', () => {
388392 isAdmin : true ,
389393 } )
390394 . then ( ( customToken ) => {
391- return firebase . auth ! ( ) . signInWithCustomToken ( customToken ) ;
395+ return clientAuth ( ) . signInWithCustomToken ( customToken ) ;
392396 } )
393397 . then ( ( { user} ) => {
394398 expect ( user ) . to . exist ;
@@ -426,7 +430,7 @@ describe('admin.auth', () => {
426430
427431 // Sign out after each test.
428432 afterEach ( ( ) => {
429- return firebase . auth ! ( ) . signOut ( ) ;
433+ return clientAuth ( ) . signOut ( ) ;
430434 } ) ;
431435
432436 // Delete test user at the end of test suite.
@@ -443,10 +447,10 @@ describe('admin.auth', () => {
443447 . then ( ( link ) => {
444448 const code = getActionCode ( link ) ;
445449 expect ( getContinueUrl ( link ) ) . equal ( actionCodeSettings . url ) ;
446- return firebase . auth ! ( ) . confirmPasswordReset ( code , newPassword ) ;
450+ return clientAuth ( ) . confirmPasswordReset ( code , newPassword ) ;
447451 } )
448452 . then ( ( ) => {
449- return firebase . auth ! ( ) . signInWithEmailAndPassword ( email , newPassword ) ;
453+ return clientAuth ( ) . signInWithEmailAndPassword ( email , newPassword ) ;
450454 } )
451455 . then ( ( result ) => {
452456 expect ( result . user ) . to . exist ;
@@ -466,10 +470,10 @@ describe('admin.auth', () => {
466470 . then ( ( link ) => {
467471 const code = getActionCode ( link ) ;
468472 expect ( getContinueUrl ( link ) ) . equal ( actionCodeSettings . url ) ;
469- return firebase . auth ! ( ) . applyActionCode ( code ) ;
473+ return clientAuth ( ) . applyActionCode ( code ) ;
470474 } )
471475 . then ( ( ) => {
472- return firebase . auth ! ( ) . signInWithEmailAndPassword ( email , userData . password ) ;
476+ return clientAuth ( ) . signInWithEmailAndPassword ( email , userData . password ) ;
473477 } )
474478 . then ( ( result ) => {
475479 expect ( result . user ) . to . exist ;
@@ -482,7 +486,7 @@ describe('admin.auth', () => {
482486 return admin . auth ( ) . generateSignInWithEmailLink ( email , actionCodeSettings )
483487 . then ( ( link ) => {
484488 expect ( getContinueUrl ( link ) ) . equal ( actionCodeSettings . url ) ;
485- return firebase . auth ! ( ) . signInWithEmailLink ( email , link ) ;
489+ return clientAuth ( ) . signInWithEmailLink ( email , link ) ;
486490 } )
487491 . then ( ( result ) => {
488492 expect ( result . user ) . to . exist ;
@@ -722,6 +726,23 @@ describe('admin.auth', () => {
722726 expect ( userRecord . uid ) . to . equal ( createdUserUid ) ;
723727 } ) ;
724728 } ) ;
729+
730+ it ( 'createCustomToken() mints a JWT that can be used to sign in tenant users' , async ( ) => {
731+ try {
732+ clientAuth ( ) . tenantId = createdTenantId ;
733+
734+ const customToken = await tenantAwareAuth . createCustomToken ( 'uid1' ) ;
735+ const { user} = await clientAuth ( ) . signInWithCustomToken ( customToken ) ;
736+ expect ( user ) . to . not . be . null ;
737+ const idToken = await user ! . getIdToken ( ) ;
738+ const token = await tenantAwareAuth . verifyIdToken ( idToken ) ;
739+
740+ expect ( token . uid ) . to . equal ( 'uid1' ) ;
741+ expect ( token . firebase . tenant ) . to . equal ( createdTenantId ) ;
742+ } finally {
743+ clientAuth ( ) . tenantId = null ;
744+ }
745+ } ) ;
725746 } ) ;
726747
727748 // Sanity check OIDC/SAML config management API.
@@ -1203,7 +1224,7 @@ describe('admin.auth', () => {
12031224
12041225 it ( 'creates a valid Firebase session cookie' , ( ) => {
12051226 return admin . auth ( ) . createCustomToken ( uid , { admin : true , groupId : '1234' } )
1206- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1227+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
12071228 . then ( ( { user} ) => {
12081229 expect ( user ) . to . exist ;
12091230 return user ! . getIdToken ( ) ;
@@ -1239,7 +1260,7 @@ describe('admin.auth', () => {
12391260 it ( 'creates a revocable session cookie' , ( ) => {
12401261 let currentSessionCookie : string ;
12411262 return admin . auth ( ) . createCustomToken ( uid2 )
1242- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1263+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
12431264 . then ( ( { user} ) => {
12441265 expect ( user ) . to . exist ;
12451266 return user ! . getIdToken ( ) ;
@@ -1266,7 +1287,7 @@ describe('admin.auth', () => {
12661287
12671288 it ( 'fails when called with a revoked ID token' , ( ) => {
12681289 return admin . auth ( ) . createCustomToken ( uid3 , { admin : true , groupId : '1234' } )
1269- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1290+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
12701291 . then ( ( { user} ) => {
12711292 expect ( user ) . to . exist ;
12721293 return user ! . getIdToken ( ) ;
@@ -1294,7 +1315,7 @@ describe('admin.auth', () => {
12941315
12951316 it ( 'fails when called with a Firebase ID token' , ( ) => {
12961317 return admin . auth ( ) . createCustomToken ( uid )
1297- . then ( ( customToken ) => firebase . auth ! ( ) . signInWithCustomToken ( customToken ) )
1318+ . then ( ( customToken ) => clientAuth ( ) . signInWithCustomToken ( customToken ) )
12981319 . then ( ( { user} ) => {
12991320 expect ( user ) . to . exist ;
13001321 return user ! . getIdToken ( ) ;
@@ -1580,7 +1601,7 @@ function testImportAndSignInUser(
15801601 expect ( result . successCount ) . to . equal ( 1 ) ;
15811602 expect ( result . errors . length ) . to . equal ( 0 ) ;
15821603 // Sign in with an email and password to the imported account.
1583- return firebase . auth ! ( ) . signInWithEmailAndPassword ( users [ 0 ] . email , rawPassword ) ;
1604+ return clientAuth ( ) . signInWithEmailAndPassword ( users [ 0 ] . email , rawPassword ) ;
15841605 } )
15851606 . then ( ( { user} ) => {
15861607 // Confirm successful sign-in.
0 commit comments