1+ import { AuthApiClient } from './auth-api-requests' ;
12import type { EmulatorEnv } from './emulator' ;
23import { useEmulator } from './emulator' ;
4+ import { AuthClientErrorCode , FirebaseAuthError } from './errors' ;
35import type { KeyStorer } from './key-store' ;
46import type { FirebaseIdToken , FirebaseTokenVerifier } from './token-verifier' ;
57import { createIdTokenVerifier , createSessionCookieVerifier } from './token-verifier' ;
8+ import { isNonNullObject , isNumber } from './validator' ;
69
710export class BaseAuth {
811 /** @internal */
912 protected readonly idTokenVerifier : FirebaseTokenVerifier ;
1013 protected readonly sessionCookieVerifier : FirebaseTokenVerifier ;
14+ private readonly authApiClient : AuthApiClient ;
1115
1216 constructor ( projectId : string , keyStore : KeyStorer ) {
1317 this . idTokenVerifier = createIdTokenVerifier ( projectId , keyStore ) ;
1418 this . sessionCookieVerifier = createSessionCookieVerifier ( projectId , keyStore ) ;
19+ this . authApiClient = new AuthApiClient ( projectId ) ;
1520 }
1621
1722 /**
@@ -31,6 +36,38 @@ export class BaseAuth {
3136 return this . idTokenVerifier . verifyJWT ( idToken , isEmulator ) ;
3237 }
3338
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+
3471 /**
3572 * Verifies a Firebase session cookie. Returns a Promise with the cookie claims.
3673 * Rejects the promise if the cookie could not be verified.
@@ -47,6 +84,9 @@ export class BaseAuth {
4784 * for code samples and detailed documentation
4885 *
4986 * @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.
5090 *
5191 * @returns A promise fulfilled with the
5292 * session cookie's decoded claims if the session cookie is valid; otherwise,
@@ -57,3 +97,15 @@ export class BaseAuth {
5797 return this . sessionCookieVerifier . verifyJWT ( sessionCookie , isEmulator ) ;
5898 }
5999}
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