@@ -2,7 +2,7 @@ import { Hono } from 'hono';
2
2
import { getCookie , setCookie } from 'hono/cookie' ;
3
3
import { csrf } from 'hono/csrf' ;
4
4
import { html } from 'hono/html' ;
5
- import { Auth , EmulatorCredential , emulatorHost , WorkersKVStoreSingle } from '../src' ;
5
+ import { Auth , ServiceAccountCredential , emulatorHost , WorkersKVStoreSingle , AdminAuthApiClient } from '../src' ;
6
6
7
7
type Env = {
8
8
EMAIL_ADDRESS : string ;
@@ -12,6 +12,9 @@ type Env = {
12
12
PUBLIC_JWK_CACHE_KEY : string ;
13
13
14
14
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 ;
15
18
} ;
16
19
17
20
const app = new Hono < { Bindings : Env } > ( ) ;
@@ -46,7 +49,7 @@ app.post('/verify-header', async c => {
46
49
c . env . PROJECT_ID ,
47
50
WorkersKVStoreSingle . getOrInitialize ( c . env . PUBLIC_JWK_CACHE_KEY , c . env . PUBLIC_JWK_CACHE_KV )
48
51
) ;
49
- const firebaseToken = await auth . verifyIdToken ( jwt , c . env ) ;
52
+ const firebaseToken = await auth . verifyIdToken ( jwt , false , c . env ) ;
50
53
51
54
return new Response ( JSON . stringify ( firebaseToken ) , {
52
55
headers : {
@@ -153,16 +156,13 @@ app.post('/admin/login_session', async c => {
153
156
// The session cookie will have the same claims as the ID token.
154
157
// To only allow session cookie setting on recent sign-in, auth_time in ID token
155
158
// 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 (
157
160
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 )
160
162
) ;
161
163
const sessionCookie = await auth . createSessionCookie (
162
164
idToken ,
163
- {
164
- expiresIn,
165
- } ,
165
+ expiresIn ,
166
166
c . env // This valus must be removed in real world
167
167
) ;
168
168
setCookie ( c , 'session' , sessionCookie , {
@@ -178,13 +178,13 @@ app.get('/admin/profile', async c => {
178
178
179
179
const auth = Auth . getOrInitialize (
180
180
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 )
183
182
) ;
184
183
185
184
try {
186
185
const decodedToken = await auth . verifySessionCookie (
187
186
session ,
187
+ false ,
188
188
c . env // This valus must be removed in real world
189
189
) ;
190
190
return c . json ( decodedToken ) ;
0 commit comments