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