Skip to content

Commit c590174

Browse files
committed
added singleton auth
1 parent 60193c7 commit c590174

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

src/auth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useEmulator } from "./emulator";
1+
import { Env, useEmulator } from "./emulator";
22
import {
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
}

src/emulator.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
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
};

src/index.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
import { BaseAuth } from "./auth"
2+
3+
14
export {
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+
}

0 commit comments

Comments
 (0)