Skip to content

Commit 03c7313

Browse files
committed
added example
1 parent c590174 commit 03c7313

File tree

6 files changed

+4402
-59
lines changed

6 files changed

+4402
-59
lines changed

example/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Auth, emulatorHost, Env } from "../src";
2+
3+
interface Bindings extends Env {
4+
EMAIL_ADDRESS: string
5+
PASSWORD: string
6+
FIREBASE_AUTH_EMULATOR_HOST: string
7+
PUBLIC_JWK_CACHE_KV: KVNamespace
8+
PROJECT_ID: string
9+
PUBLIC_JWK_CACHE_KEY: string
10+
}
11+
12+
const signInPath = "/identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=test1234"
13+
14+
export async function handleRequest(req: Request, env: Bindings) {
15+
const url = new URL(req.url)
16+
const firebaseEmuHost = emulatorHost(env)
17+
if (url.pathname === "/get-jwt" && !!firebaseEmuHost) {
18+
const firebaseEmulatorSignInUrl = "http://" + firebaseEmuHost + signInPath
19+
const resp = await fetch(firebaseEmulatorSignInUrl, {
20+
method: "POST",
21+
body: JSON.stringify({
22+
email: env.EMAIL_ADDRESS,
23+
password: env.PASSWORD,
24+
returnSecureToken: true,
25+
}),
26+
headers: {
27+
"Content-Type": "application/json"
28+
}
29+
})
30+
return resp
31+
}
32+
33+
const authorization = req.headers.get('Authorization')
34+
if (authorization === null) {
35+
return new Response(null, {
36+
status: 400,
37+
})
38+
}
39+
const jwt = authorization.replace(/Bearer\s+/i, "")
40+
const auth = Auth.getOrInitialize(
41+
env.PROJECT_ID,
42+
env.PUBLIC_JWK_CACHE_KEY,
43+
env.PUBLIC_JWK_CACHE_KV
44+
)
45+
const firebaseToken = await auth.verifyIdToken(jwt, env)
46+
47+
return new Response(JSON.stringify(firebaseToken), {
48+
headers: {
49+
"Content-Type": "application/json"
50+
}
51+
})
52+
}
53+
54+
export default { fetch: handleRequest };

example/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["@cloudflare/workers-types"]
5+
},
6+
"include": ["../src/**/*", "**/*"]
7+
}

example/wrangler.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name = "firebase-auth-example"
2+
compatibility_date = "2022-07-05"
3+
workers_dev = true
4+
5+
[vars]
6+
# Please set FIREBASE_AUTH_EMULATOR_HOST environment variable in your wrangler.toml.
7+
# see: https://developers.cloudflare.com/workers/platform/environment-variables/#environment-variables-via-wrangler
8+
#
9+
# Example for wrangler.toml
10+
# [vars]
11+
# FIREBASE_AUTH_EMULATOR_HOST = "localhost:8080"
12+
#
13+
# Override values for `--env production` usage
14+
# [env.production.vars]
15+
# FIREBASE_AUTH_EMULATOR_HOST = ""
16+
FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
17+
18+
# Setup user account in Emulator UI
19+
EMAIL_ADDRESS = "[email protected]"
20+
PASSWORD = "test1234"
21+
22+
PROJECT_ID = "example-project12345" # see package.json (for emulator)
23+
24+
# Specify cache key to store and get public jwk.
25+
PUBLIC_JWK_CACHE_KEY = "public-jwk-cache-key"
26+
27+
kv_namespaces = [
28+
{ binding = "PUBLIC_JWK_CACHE_KV", id = "", preview_id = "" }
29+
]
30+
31+
tsconfig = "./tsconfig.json"

firebase.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"emulators": {
3+
"auth": {
4+
"port": 9099
5+
},
6+
"ui": {
7+
"enabled": true
8+
}
9+
}
10+
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
"author": "Kei Kamikawa <[email protected]>",
66
"license": "MIT",
77
"scripts": {
8-
"test": "jest"
8+
"test": "jest",
9+
"start-firebase-emulator": "firebase emulators:start --project example-project12345",
10+
"start-example": "wrangler dev example/index.ts --config=example/wrangler.toml --local=true"
911
},
1012
"dependencies": {},
1113
"devDependencies": {
1214
"@cloudflare/workers-types": "^3.14.0",
1315
"@types/jest": "^28.1.3",
16+
"firebase-tools": "^11.2.0",
1417
"jest": "^28.1.2",
1518
"jest-environment-miniflare": "^2.5.1",
1619
"ts-jest": "^28.0.5",
17-
"typescript": "^4.7.4"
20+
"typescript": "^4.7.4",
21+
"wrangler": "^2.0.15"
1822
}
1923
}

0 commit comments

Comments
 (0)