File tree Expand file tree Collapse file tree 3 files changed +44
-22
lines changed
Expand file tree Collapse file tree 3 files changed +44
-22
lines changed Original file line number Diff line number Diff line change 1- import { useEmulator } from "./emulator" ;
1+ import { Env , useEmulator } from "./emulator" ;
22import {
33 createIdTokenVerifier ,
44 FirebaseIdToken ,
55 FirebaseTokenVerifier ,
66} from "./token-verifier" ;
77
8- export class Auth {
8+ export class BaseAuth {
99 /** @internal */
1010 protected readonly idTokenVerifier : FirebaseTokenVerifier ;
1111
@@ -33,8 +33,8 @@ export class Auth {
3333 * token's decoded claims if the ID token is valid; otherwise, a rejected
3434 * promise.
3535 */
36- public verifyIdToken ( idToken : string ) : Promise < FirebaseIdToken > {
37- const isEmulator = useEmulator ( ) ;
36+ public verifyIdToken ( idToken : string , env ?: Env ) : Promise < FirebaseIdToken > {
37+ const isEmulator = useEmulator ( env ) ;
3838 return this . idTokenVerifier . verifyJWT ( idToken , isEmulator ) ;
3939 }
4040}
Original file line number Diff line number Diff line change 1- declare global {
2- // Please set FIREBASE_AUTH_EMULATOR_HOST environment variable in your wrangler.toml.
3- // see: https://developers.cloudflare.com/workers/platform/environment-variables/#environment-variables-via-wrangler
4- //
5- // Example for wrangler.toml
6- // [vars]
7- // FIREBASE_AUTH_EMULATOR_HOST = "localhost:8080"
8- //
9- // # Override values for `--env production` usage
10- // [env.production.vars]
11- // FIREBASE_AUTH_EMULATOR_HOST = ""
12- const FIREBASE_AUTH_EMULATOR_HOST : string | undefined ;
1+ export interface Env {
2+ FIREBASE_AUTH_EMULATOR_HOST : string | undefined
133}
144
15- function emulatorHost ( ) : string | undefined {
16- return FIREBASE_AUTH_EMULATOR_HOST ;
5+ export function emulatorHost ( env ?: Env ) : string | undefined {
6+ return env ?. FIREBASE_AUTH_EMULATOR_HOST ;
177}
188
199/**
2010 * When true the SDK should communicate with the Auth Emulator for all API
2111 * calls and also produce unsigned tokens.
2212 */
23- export const useEmulator = ( ) : boolean => {
24- return ! ! emulatorHost ( ) ;
13+ export const useEmulator = ( env ?: Env ) : boolean => {
14+ return ! ! emulatorHost ( env ) ;
2515} ;
Original file line number Diff line number Diff line change 1+ import { BaseAuth } from "./auth"
2+
3+
14export {
2- Auth
3- } from "./auth"
5+ emulatorHost ,
6+ useEmulator
7+ } from "./emulator"
8+ export type { Env } from './emulator'
9+
10+ export class Auth extends BaseAuth {
11+ private static instance ?: Auth ;
12+
13+ private constructor (
14+ projectId : string ,
15+ cacheKey : string ,
16+ cfPublicKeyCacheNamespace : KVNamespace
17+ ) {
18+ super ( projectId , cacheKey , cfPublicKeyCacheNamespace )
19+ }
20+
21+ static getOrInitialize (
22+ projectId : string ,
23+ cacheKey : string ,
24+ cfPublicKeyCacheNamespace : KVNamespace
25+ ) : Auth {
26+ if ( ! Auth . instance ) {
27+ Auth . instance = new Auth (
28+ projectId ,
29+ cacheKey ,
30+ cfPublicKeyCacheNamespace ,
31+ )
32+ }
33+ return Auth . instance
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments