Skip to content

Commit 9306a28

Browse files
committed
fixed example
1 parent 161fc97 commit 9306a28

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

example/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Hono } from 'hono';
22
import { getCookie, setCookie } from 'hono/cookie';
33
import { csrf } from 'hono/csrf';
44
import { html } from 'hono/html';
5-
import { Auth, EmulatorCredential, emulatorHost, WorkersKVStoreSingle } from '../src';
5+
import { Auth, ServiceAccountCredential, emulatorHost, WorkersKVStoreSingle, AdminAuthApiClient } from '../src';
66

77
type Env = {
88
EMAIL_ADDRESS: string;
@@ -12,6 +12,9 @@ type Env = {
1212
PUBLIC_JWK_CACHE_KEY: string;
1313

1414
FIREBASE_AUTH_EMULATOR_HOST: string; // satisfied EmulatorEnv
15+
// Set JSON as string.
16+
// See: https://cloud.google.com/iam/docs/keys-create-delete
17+
SERVICE_ACCOUNT_JSON: string;
1518
};
1619

1720
const app = new Hono<{ Bindings: Env }>();
@@ -46,7 +49,7 @@ app.post('/verify-header', async c => {
4649
c.env.PROJECT_ID,
4750
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV)
4851
);
49-
const firebaseToken = await auth.verifyIdToken(jwt, c.env);
52+
const firebaseToken = await auth.verifyIdToken(jwt, false, c.env);
5053

5154
return new Response(JSON.stringify(firebaseToken), {
5255
headers: {
@@ -153,16 +156,13 @@ app.post('/admin/login_session', async c => {
153156
// The session cookie will have the same claims as the ID token.
154157
// To only allow session cookie setting on recent sign-in, auth_time in ID token
155158
// can be checked to ensure user was recently signed in before creating a session cookie.
156-
const auth = Auth.getOrInitialize(
159+
const auth = AdminAuthApiClient.getOrInitialize(
157160
c.env.PROJECT_ID,
158-
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV),
159-
new EmulatorCredential() // You MUST use ServiceAccountCredential in real world
161+
new ServiceAccountCredential(c.env.SERVICE_ACCOUNT_JSON)
160162
);
161163
const sessionCookie = await auth.createSessionCookie(
162164
idToken,
163-
{
164-
expiresIn,
165-
},
165+
expiresIn,
166166
c.env // This valus must be removed in real world
167167
);
168168
setCookie(c, 'session', sessionCookie, {
@@ -178,13 +178,13 @@ app.get('/admin/profile', async c => {
178178

179179
const auth = Auth.getOrInitialize(
180180
c.env.PROJECT_ID,
181-
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV),
182-
new EmulatorCredential() // You MUST use ServiceAccountCredential in real world
181+
WorkersKVStoreSingle.getOrInitialize(c.env.PUBLIC_JWK_CACHE_KEY, c.env.PUBLIC_JWK_CACHE_KV)
183182
);
184183

185184
try {
186185
const decodedToken = await auth.verifySessionCookie(
187186
session,
187+
false,
188188
c.env // This valus must be removed in real world
189189
);
190190
return c.json(decodedToken);

example/wrangler.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ tsconfig = "./tsconfig.json"
1818
# FIREBASE_AUTH_EMULATOR_HOST = ""
1919
FIREBASE_AUTH_EMULATOR_HOST = "127.0.0.1:9099"
2020

21+
# See: https://cloud.google.com/iam/docs/keys-create-delete
22+
SERVICE_ACCOUNT_JSON = "{\"type\":\"service_account\",\"project_id\":\"project12345\",\"private_key_id\":\"xxxxxxxxxxxxxxxxx\",\"private_key\":\"-----BEGIN PRIVATE KEY-----XXXXXX-----END PRIVATE KEY-----\n\",\"client_email\":\"xxxxx@xxxxxx.iam.gserviceaccount.com\",\"client_id\":\"xxxxxx\",\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\",\"token_uri\":\"https://oauth2.googleapis.com/token\",\"auth_provider_x509_cert_url\":\"https://www.googleapis.com/oauth2/v1/certs\",\"client_x509_cert_url\":\"https://www.googleapis.com/robot/v1/metadata/x509/xxxxx@xxxxxx.iam.gserviceaccount.com\"}"
23+
2124
# Setup user account in Emulator UI
2225
EMAIL_ADDRESS = "test@example.com"
2326
PASSWORD = "test1234"

0 commit comments

Comments
 (0)