Skip to content

Commit fb09fae

Browse files
committed
use hono for example
1 parent 49001a5 commit fb09fae

File tree

6 files changed

+48
-35
lines changed

6 files changed

+48
-35
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ lerna-debug.log*
1717
*.tsbuildinfo
1818

1919
# Dependency directories
20-
node_modules/
20+
node_modules/
21+
.wrangler/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ Interface representing a decoded Firebase ID token, returned from the `authObj.v
185185
I put an [example](https://github.com/Code-Hex/firebase-auth-cloudflare-workers/tree/master/example) directory as Module Worker Syntax. this is explanation how to run the code.
186186

187187
1. Clone this repository and change your directory to it.
188-
2. Install dev dependencies as `yarn` command.
189-
3. Run firebase auth emulator by `$ yarn start-firebase-emulator`
188+
2. Install dev dependencies as `pnpm` command.
189+
3. Run firebase auth emulator by `$ pnpm start-firebase-emulator`
190190
4. Access to Emulator UI in your favorite browser.
191191
5. Create a new user on Emulator UI. (email: `[email protected]` password: `test1234`)
192-
6. Run example code on local (may serve as `localhost:8787`) by `$ yarn start-example`
192+
6. Run example code on local (may serve as `localhost:8787`) by `$ pnpm start-example`
193193
7. Get jwt for created user by `$ curl -s http://localhost:8787/get-jwt | jq .idToken -r`
194194
8. Try authorization with user jwt `$ curl http://localhost:8787/ -H 'Authorization: Bearer PASTE-JWT-HERE'`
195195

example/index.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
1-
import type { EmulatorEnv } from '../src';
1+
import { Hono } from 'hono';
22
import { Auth, emulatorHost, WorkersKVStoreSingle } from '../src';
33

4-
interface Bindings extends EmulatorEnv {
4+
type Env = {
55
EMAIL_ADDRESS: string;
66
PASSWORD: string;
7-
FIREBASE_AUTH_EMULATOR_HOST: string;
87
PUBLIC_JWK_CACHE_KV: KVNamespace;
98
PROJECT_ID: string;
109
PUBLIC_JWK_CACHE_KEY: string;
11-
}
10+
11+
FIREBASE_AUTH_EMULATOR_HOST: string; // satisfied EmulatorEnv
12+
};
13+
14+
const app = new Hono<{ Bindings: Env }>();
1215

1316
const signInPath = '/identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=test1234';
1417

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+
});
3333

34-
const authorization = req.headers.get('Authorization');
34+
app.post('/verify-header', async c => {
35+
const authorization = c.req.raw.headers.get('Authorization');
3536
if (authorization === null) {
3637
return new Response(null, {
3738
status: 400,
3839
});
3940
}
4041
const jwt = authorization.replace(/Bearer\s+/i, '');
4142
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)
4445
);
45-
const firebaseToken = await auth.verifyIdToken(jwt, env);
46+
const firebaseToken = await auth.verifyIdToken(jwt, c.env);
4647

4748
return new Response(JSON.stringify(firebaseToken), {
4849
headers: {
4950
'Content-Type': 'application/json',
5051
},
5152
});
52-
}
53+
});
5354

54-
export default { fetch: handleRequest };
55+
export default app;

example/wrangler.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name = "firebase-auth-example"
2-
compatibility_date = "2022-07-05"
2+
compatibility_date = "2023-12-01"
33
workers_dev = true
4+
main = "index.ts"
45

56
tsconfig = "./tsconfig.json"
67

@@ -28,5 +29,5 @@ PUBLIC_JWK_CACHE_KEY = "public-jwk-cache-key"
2829

2930
[[kv_namespaces]]
3031
binding = "PUBLIC_JWK_CACHE_KV"
31-
id = ""
32+
id = "testingId"
3233
preview_id = "testingId"

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"prettier:check": "prettier --check \"**/*.ts\"",
2525
"lint": "eslint --ext .ts .",
2626
"lint-fix": "eslint --fix --ext .ts .",
27-
"prepublish": "run-p build:*"
27+
"prepublish": "run-p build:*",
28+
"wrangler": "wrangler"
2829
},
2930
"devDependencies": {
3031
"@cloudflare/workers-types": "^4.20240208.0",
@@ -37,6 +38,7 @@
3738
"eslint-plugin-eslint-comments": "^3.2.0",
3839
"eslint-plugin-import": "^2.29.1",
3940
"firebase-tools": "^13.3.0",
41+
"hono": "^4.0.4",
4042
"miniflare": "^3.20240129.3",
4143
"npm-run-all": "^4.1.5",
4244
"prettier": "^3.2.5",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)