1
+ import { AuthApiClient } from './auth-api-requests' ;
1
2
import type { EmulatorEnv } from './emulator' ;
2
3
import { useEmulator } from './emulator' ;
4
+ import { AuthClientErrorCode , FirebaseAuthError } from './errors' ;
3
5
import type { KeyStorer } from './key-store' ;
4
6
import type { FirebaseIdToken , FirebaseTokenVerifier } from './token-verifier' ;
5
7
import { createIdTokenVerifier , createSessionCookieVerifier } from './token-verifier' ;
8
+ import { isNonNullObject , isNumber } from './validator' ;
6
9
7
10
export class BaseAuth {
8
11
/** @internal */
9
12
protected readonly idTokenVerifier : FirebaseTokenVerifier ;
10
13
protected readonly sessionCookieVerifier : FirebaseTokenVerifier ;
14
+ private readonly authApiClient : AuthApiClient ;
11
15
12
16
constructor ( projectId : string , keyStore : KeyStorer ) {
13
17
this . idTokenVerifier = createIdTokenVerifier ( projectId , keyStore ) ;
14
18
this . sessionCookieVerifier = createSessionCookieVerifier ( projectId , keyStore ) ;
19
+ this . authApiClient = new AuthApiClient ( projectId ) ;
15
20
}
16
21
17
22
/**
@@ -31,6 +36,38 @@ export class BaseAuth {
31
36
return this . idTokenVerifier . verifyJWT ( idToken , isEmulator ) ;
32
37
}
33
38
39
+ /**
40
+ * Creates a new Firebase session cookie with the specified options. The created
41
+ * JWT string can be set as a server-side session cookie with a custom cookie
42
+ * policy, and be used for session management. The session cookie JWT will have
43
+ * the same payload claims as the provided ID token.
44
+ *
45
+ * See {@link https://firebase.google.com/docs/auth/admin/manage-cookies | Manage Session Cookies}
46
+ * for code samples and detailed documentation.
47
+ *
48
+ * @param idToken - The Firebase ID token to exchange for a session
49
+ * cookie.
50
+ * @param sessionCookieOptions - The session
51
+ * cookie options which includes custom session duration.
52
+ * @param env - An optional parameter specifying the environment in which the function is running.
53
+ * If the function is running in an emulator environment, this should be set to `EmulatorEnv`.
54
+ * If not specified, the function will assume it is running in a production environment.
55
+ *
56
+ * @returns A promise that resolves on success with the
57
+ * created session cookie.
58
+ */
59
+ public createSessionCookie (
60
+ idToken : string ,
61
+ sessionCookieOptions : SessionCookieOptions ,
62
+ env ?: EmulatorEnv
63
+ ) : Promise < string > {
64
+ // Return rejected promise if expiresIn is not available.
65
+ if ( ! isNonNullObject ( sessionCookieOptions ) || ! isNumber ( sessionCookieOptions . expiresIn ) ) {
66
+ return Promise . reject ( new FirebaseAuthError ( AuthClientErrorCode . INVALID_SESSION_COOKIE_DURATION ) ) ;
67
+ }
68
+ return this . authApiClient . createSessionCookie ( idToken , sessionCookieOptions . expiresIn , env ) ;
69
+ }
70
+
34
71
/**
35
72
* Verifies a Firebase session cookie. Returns a Promise with the cookie claims.
36
73
* Rejects the promise if the cookie could not be verified.
@@ -47,6 +84,9 @@ export class BaseAuth {
47
84
* for code samples and detailed documentation
48
85
*
49
86
* @param sessionCookie - The session cookie to verify.
87
+ * @param env - An optional parameter specifying the environment in which the function is running.
88
+ * If the function is running in an emulator environment, this should be set to `EmulatorEnv`.
89
+ * If not specified, the function will assume it is running in a production environment.
50
90
*
51
91
* @returns A promise fulfilled with the
52
92
* session cookie's decoded claims if the session cookie is valid; otherwise,
@@ -57,3 +97,15 @@ export class BaseAuth {
57
97
return this . sessionCookieVerifier . verifyJWT ( sessionCookie , isEmulator ) ;
58
98
}
59
99
}
100
+
101
+ /**
102
+ * Interface representing the session cookie options needed for the
103
+ * {@link BaseAuth.createSessionCookie} method.
104
+ */
105
+ export interface SessionCookieOptions {
106
+ /**
107
+ * The session cookie custom expiration in milliseconds. The minimum allowed is
108
+ * 5 minutes and the maxium allowed is 2 weeks.
109
+ */
110
+ expiresIn : number ;
111
+ }
0 commit comments