@@ -2,7 +2,7 @@ import { Hono } from 'hono';
22import { getCookie , setCookie } from 'hono/cookie' ;
33import { csrf } from 'hono/csrf' ;
44import { html } from 'hono/html' ;
5- import { Auth , EmulatorCredential , emulatorHost , WorkersKVStoreSingle } from '../src' ;
5+ import { Auth , ServiceAccountCredential , emulatorHost , WorkersKVStoreSingle , AdminAuthApiClient } from '../src' ;
66
77type 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
1720const 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 ) ;
0 commit comments