firebase-admin-edge 1.0.11 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -7
- package/dist/auth/firebase-admin-auth.js +33 -33
- package/dist/auth/firebase-admin-auth.js.map +1 -1
- package/dist/auth/firebase-admin-auth.test.js +71 -71
- package/dist/auth/firebase-admin-auth.test.js.map +1 -1
- package/dist/auth/firebase-auth-endpoints.js +27 -27
- package/dist/auth/firebase-auth-endpoints.test.js +29 -29
- package/dist/auth/firebase-auth-endpoints.test.js.map +1 -1
- package/dist/auth/firebase-auth.js +7 -7
- package/dist/auth/firebase-auth.js.map +1 -1
- package/dist/auth/firebase-auth.test.js +9 -9
- package/dist/auth/firebase-jwt.js +32 -32
- package/dist/auth/firebase-jwt.js.map +1 -1
- package/dist/auth/firebase-jwt.test.js +14 -14
- package/dist/auth/firebase-jwt.test.js.map +1 -1
- package/dist/auth/github-oauth.d.ts +16 -0
- package/dist/auth/github-oauth.d.ts.map +1 -0
- package/dist/auth/github-oauth.js +31 -0
- package/dist/auth/github-oauth.js.map +1 -0
- package/dist/auth/google-oauth.d.ts.map +1 -1
- package/dist/auth/google-oauth.js +16 -15
- package/dist/auth/google-oauth.js.map +1 -1
- package/dist/auth/google-oauth.test.js +15 -14
- package/dist/auth/google-oauth.test.js.map +1 -1
- package/dist/firebase-edge-server.d.ts +14 -74
- package/dist/firebase-edge-server.d.ts.map +1 -1
- package/dist/firebase-edge-server.js +105 -278
- package/dist/firebase-edge-server.js.map +1 -1
- package/dist/firebase-edge-server.test.js +115 -76
- package/dist/firebase-edge-server.test.js.map +1 -1
- package/dist/rest-fetch.d.ts.map +1 -1
- package/dist/rest-fetch.js +7 -6
- package/dist/rest-fetch.js.map +1 -1
- package/dist/rest-fetch.test.js +48 -30
- package/dist/rest-fetch.test.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { FirebaseAdminAuth } from './auth/firebase-admin-auth.js';
|
|
2
2
|
import { FirebaseAuth } from './auth/firebase-auth.js';
|
|
3
3
|
import { signJWTCustomToken } from './auth/firebase-jwt.js';
|
|
4
|
-
import {
|
|
4
|
+
import { createGitHubOAuthLoginUrl, exchangeCodeForGitHubIdToken } from './auth/github-oauth.js';
|
|
5
|
+
import { createGoogleOAuthLoginUrl, exchangeCodeForGoogleIdToken } from './auth/google-oauth.js';
|
|
5
6
|
const DEFAULT_SESSION_NAME = '__session';
|
|
6
7
|
const COOKIE_OPTIONS = {
|
|
7
8
|
httpOnly: true,
|
|
8
9
|
secure: true,
|
|
9
10
|
sameSite: 'lax',
|
|
10
11
|
path: '/',
|
|
11
|
-
maxAge: 60 * 60 * 24 * 5 * 1000
|
|
12
|
+
maxAge: 60 * 60 * 24 * 5 * 1000
|
|
12
13
|
};
|
|
13
14
|
/**
|
|
14
|
-
* Official Firebase OAuth providers
|
|
15
|
-
* @constant
|
|
16
|
-
* @type {readonly string[]}
|
|
15
|
+
* Official Firebase OAuth providers
|
|
17
16
|
*/
|
|
18
17
|
export const OFFICIAL_FIREBASE_OAUTH_PROVIDERS = [
|
|
19
18
|
'google',
|
|
@@ -23,65 +22,20 @@ export const OFFICIAL_FIREBASE_OAUTH_PROVIDERS = [
|
|
|
23
22
|
'github',
|
|
24
23
|
'microsoft',
|
|
25
24
|
'yahoo',
|
|
26
|
-
'playgames'
|
|
25
|
+
'playgames'
|
|
27
26
|
];
|
|
28
27
|
/**
|
|
29
|
-
* Creates a Firebase Edge Server
|
|
28
|
+
* Creates a Firebase Edge Server for authentication and session management in edge environments.
|
|
30
29
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @param config
|
|
35
|
-
* @param config.
|
|
36
|
-
* @param config.
|
|
37
|
-
* @
|
|
38
|
-
* @param config.cookies - Cookie management configuration for session handling
|
|
39
|
-
* @param config.fetch - Optional custom fetch implementation (defaults to globalThis.fetch)
|
|
40
|
-
*
|
|
41
|
-
* @returns Object containing authentication methods and utilities
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* const server = createFirebaseEdgeServer({
|
|
46
|
-
* serviceAccount: {
|
|
47
|
-
* type: "service_account",
|
|
48
|
-
* project_id: "your-project-id",
|
|
49
|
-
* private_key_id: "...",
|
|
50
|
-
* private_key: "...",
|
|
51
|
-
* client_email: "...",
|
|
52
|
-
* client_id: "...",
|
|
53
|
-
* auth_uri: "...",
|
|
54
|
-
* token_uri: "...",
|
|
55
|
-
* auth_provider_x509_cert_url: "...",
|
|
56
|
-
* client_x509_cert_url: "..."
|
|
57
|
-
* },
|
|
58
|
-
* firebaseConfig: {
|
|
59
|
-
* apiKey: "your-api-key",
|
|
60
|
-
* authDomain: "your-project.firebaseapp.com",
|
|
61
|
-
* projectId: "your-project-id"
|
|
62
|
-
* },
|
|
63
|
-
* providers: {
|
|
64
|
-
* google: {
|
|
65
|
-
* client_id: "your-google-client-id",
|
|
66
|
-
* client_secret: "your-google-client-secret"
|
|
67
|
-
* }
|
|
68
|
-
* },
|
|
69
|
-
* cookies: {
|
|
70
|
-
* getSession: (name) => getCookie(name),
|
|
71
|
-
* saveSession: (name, value, options) => setCookie(name, value, options)
|
|
72
|
-
* }
|
|
73
|
-
* });
|
|
74
|
-
*
|
|
75
|
-
* // Using guard clauses for clean error handling
|
|
76
|
-
* const { data: user, error } = await server.getUser();
|
|
77
|
-
* if (error) return handleError(error);
|
|
78
|
-
* if (!user) return redirectToLogin();
|
|
79
|
-
*
|
|
80
|
-
* // User is authenticated, proceed with protected logic
|
|
81
|
-
* console.log('Authenticated user:', user.email);
|
|
82
|
-
* ```
|
|
30
|
+
* @param config Configuration object
|
|
31
|
+
* @param config.serviceAccount Firebase service account for admin operations
|
|
32
|
+
* @param config.firebaseConfig Firebase client configuration
|
|
33
|
+
* @param config.providers OAuth provider configurations (Google, GitHub, etc.)
|
|
34
|
+
* @param config.cookies Cookie management functions
|
|
35
|
+
* @param config.fetch Optional custom fetch implementation
|
|
36
|
+
* @returns Object with authentication methods
|
|
83
37
|
*/
|
|
84
|
-
export function createFirebaseEdgeServer({ serviceAccount, firebaseConfig, providers, cookies, fetch
|
|
38
|
+
export function createFirebaseEdgeServer({ serviceAccount, firebaseConfig, providers, cookies, fetch }) {
|
|
85
39
|
const sessionName = cookies.sessionName || DEFAULT_SESSION_NAME;
|
|
86
40
|
const getSession = cookies.getSession;
|
|
87
41
|
const saveSession = cookies.saveSession;
|
|
@@ -89,78 +43,35 @@ export function createFirebaseEdgeServer({ serviceAccount, firebaseConfig, provi
|
|
|
89
43
|
const auth = new FirebaseAuth(firebaseConfig, fetchImpl);
|
|
90
44
|
const adminAuth = new FirebaseAdminAuth(serviceAccount, fetchImpl);
|
|
91
45
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* This is an internal helper function used by signOut and error handlers to ensure
|
|
95
|
-
* clean session management when authentication fails or users sign out.
|
|
96
|
-
*
|
|
46
|
+
* Clears the session cookie
|
|
97
47
|
* @internal
|
|
98
48
|
*/
|
|
99
49
|
function deleteSession() {
|
|
100
50
|
saveSession(sessionName, '', {
|
|
101
51
|
...COOKIE_OPTIONS,
|
|
102
|
-
maxAge: 0
|
|
52
|
+
maxAge: 0
|
|
103
53
|
});
|
|
104
54
|
}
|
|
105
55
|
/**
|
|
106
|
-
* Signs out the current user by
|
|
107
|
-
*
|
|
108
|
-
* This method only removes the local session and does not revoke the user's Firebase tokens.
|
|
109
|
-
* For complete logout, consider also calling Firebase client-side signOut if needed.
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```typescript
|
|
113
|
-
* // Simple sign out
|
|
114
|
-
* server.signOut();
|
|
115
|
-
*
|
|
116
|
-
* // With redirect after sign out
|
|
117
|
-
* server.signOut();
|
|
118
|
-
* return redirect('/login');
|
|
119
|
-
* ```
|
|
56
|
+
* Signs out the current user by clearing the session cookie.
|
|
57
|
+
* Note: This only removes the server-side session, not Firebase client tokens.
|
|
120
58
|
*/
|
|
121
59
|
function signOut() {
|
|
122
60
|
deleteSession();
|
|
123
61
|
return;
|
|
124
62
|
}
|
|
125
63
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* Validates the session cookie and returns the decoded user token. If the session
|
|
129
|
-
* is invalid or expired, automatically cleans up the session cookie.
|
|
130
|
-
*
|
|
131
|
-
* @param checkRevoked - Whether to check if the user's token has been revoked (default: false)
|
|
132
|
-
* @returns Promise resolving to user data and error information
|
|
64
|
+
* Gets the current authenticated user from the session cookie.
|
|
133
65
|
*
|
|
134
|
-
* @
|
|
135
|
-
*
|
|
136
|
-
* // Using guard clauses for clean error handling
|
|
137
|
-
* const { data: user, error } = await server.getUser();
|
|
138
|
-
* if (error) {
|
|
139
|
-
* console.error('Authentication error:', error);
|
|
140
|
-
* return redirect('/login');
|
|
141
|
-
* }
|
|
142
|
-
*
|
|
143
|
-
* if (!user) {
|
|
144
|
-
* console.log('No user session found');
|
|
145
|
-
* return redirect('/login');
|
|
146
|
-
* }
|
|
147
|
-
*
|
|
148
|
-
* // User is authenticated, proceed safely
|
|
149
|
-
* console.log('User ID:', user.sub);
|
|
150
|
-
* console.log('Email:', user.email);
|
|
151
|
-
* console.log('Email verified:', user.email_verified);
|
|
152
|
-
*
|
|
153
|
-
* // Check for revoked tokens in sensitive operations
|
|
154
|
-
* const { data: verifiedUser, error: revokeError } = await server.getUser(true);
|
|
155
|
-
* if (revokeError) return handleRevokedToken();
|
|
156
|
-
* ```
|
|
66
|
+
* @param checkRevoked Whether to check if the token has been revoked
|
|
67
|
+
* @returns Promise with user data and error
|
|
157
68
|
*/
|
|
158
69
|
async function getUser(checkRevoked = false) {
|
|
159
70
|
const sessionCookie = await getSession(sessionName);
|
|
160
71
|
if (!sessionCookie) {
|
|
161
72
|
return {
|
|
162
73
|
data: null,
|
|
163
|
-
error: null
|
|
74
|
+
error: null
|
|
164
75
|
};
|
|
165
76
|
}
|
|
166
77
|
const { data: decodedToken, error: verifyError } = await adminAuth.verifySessionCookie(sessionCookie, checkRevoked);
|
|
@@ -168,61 +79,28 @@ export function createFirebaseEdgeServer({ serviceAccount, firebaseConfig, provi
|
|
|
168
79
|
deleteSession();
|
|
169
80
|
return {
|
|
170
81
|
data: null,
|
|
171
|
-
error: verifyError
|
|
82
|
+
error: verifyError
|
|
172
83
|
};
|
|
173
84
|
}
|
|
174
85
|
if (!decodedToken) {
|
|
175
86
|
deleteSession();
|
|
176
87
|
return {
|
|
177
88
|
data: null,
|
|
178
|
-
error: null
|
|
89
|
+
error: null
|
|
179
90
|
};
|
|
180
91
|
}
|
|
181
92
|
return {
|
|
182
93
|
data: decodedToken,
|
|
183
|
-
error: null
|
|
94
|
+
error: null
|
|
184
95
|
};
|
|
185
96
|
}
|
|
186
97
|
/**
|
|
187
|
-
* Generates a Google OAuth login URL
|
|
188
|
-
*
|
|
189
|
-
* Automatically clears any existing session before generating the URL to ensure
|
|
190
|
-
* a clean authentication flow. The generated URL will redirect users to Google's
|
|
191
|
-
* OAuth consent screen.
|
|
192
|
-
*
|
|
193
|
-
* @param redirect_uri - The URI to redirect to after Google authentication (must be registered in Google Console)
|
|
194
|
-
* @param path - Additional path parameter for the OAuth flow state management
|
|
195
|
-
* @returns Promise resolving to the Google OAuth login URL
|
|
196
|
-
*
|
|
197
|
-
* @throws {Error} If Google provider is not configured in the providers config
|
|
98
|
+
* Generates a Google OAuth login URL and clears any existing session.
|
|
198
99
|
*
|
|
199
|
-
* @
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* const loginUrl = await server.getGoogleLoginURL(
|
|
204
|
-
* 'https://yourapp.com/auth/callback',
|
|
205
|
-
* '/dashboard'
|
|
206
|
-
* );
|
|
207
|
-
* return redirect(loginUrl);
|
|
208
|
-
* } catch (error) {
|
|
209
|
-
* console.error('Google OAuth not configured:', error);
|
|
210
|
-
* return errorResponse('OAuth unavailable');
|
|
211
|
-
* }
|
|
212
|
-
*
|
|
213
|
-
* // In a route handler
|
|
214
|
-
* export async function GET(request: Request) {
|
|
215
|
-
* const url = new URL(request.url);
|
|
216
|
-
* const returnTo = url.searchParams.get('returnTo') || '/dashboard';
|
|
217
|
-
*
|
|
218
|
-
* const loginUrl = await server.getGoogleLoginURL(
|
|
219
|
-
* `${url.origin}/auth/callback`,
|
|
220
|
-
* returnTo
|
|
221
|
-
* );
|
|
222
|
-
*
|
|
223
|
-
* return redirect(loginUrl);
|
|
224
|
-
* }
|
|
225
|
-
* ```
|
|
100
|
+
* @param redirect_uri OAuth redirect URI
|
|
101
|
+
* @param path State parameter for the OAuth flow
|
|
102
|
+
* @returns Google OAuth login URL
|
|
103
|
+
* @throws Error if Google provider not configured
|
|
226
104
|
*/
|
|
227
105
|
async function getGoogleLoginURL(redirect_uri, path) {
|
|
228
106
|
deleteSession();
|
|
@@ -233,211 +111,160 @@ export function createFirebaseEdgeServer({ serviceAccount, firebaseConfig, provi
|
|
|
233
111
|
return createGoogleOAuthLoginUrl(redirect_uri, path, client_id);
|
|
234
112
|
}
|
|
235
113
|
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* This method should be called in your OAuth callback handler to complete the authentication
|
|
239
|
-
* flow initiated by getGoogleLoginURL. It exchanges the authorization code for tokens,
|
|
240
|
-
* creates a Firebase session, and sets the session cookie.
|
|
241
|
-
*
|
|
242
|
-
* @param code - The authorization code received from Google OAuth callback
|
|
243
|
-
* @param redirect_uri - The redirect URI used in the OAuth flow (must match the one used in getGoogleLoginURL)
|
|
244
|
-
* @returns Promise resolving to success/error information
|
|
245
|
-
*
|
|
246
|
-
* @example
|
|
247
|
-
* ```typescript
|
|
248
|
-
* // In your OAuth callback handler with guard clauses
|
|
249
|
-
* export async function GET(request: Request) {
|
|
250
|
-
* const url = new URL(request.url);
|
|
251
|
-
* const code = url.searchParams.get('code');
|
|
252
|
-
* const state = url.searchParams.get('state');
|
|
114
|
+
* Generates a GitHub OAuth login URL and clears any existing session.
|
|
253
115
|
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* // Success - redirect to protected area
|
|
271
|
-
* const redirectTo = state || '/dashboard';
|
|
272
|
-
* return redirect(redirectTo);
|
|
273
|
-
* }
|
|
116
|
+
* @param redirect_uri OAuth redirect URI
|
|
117
|
+
* @param path State parameter for the OAuth flow
|
|
118
|
+
* @returns GitHub OAuth login URL
|
|
119
|
+
* @throws Error if GitHub provider not configured
|
|
120
|
+
*/
|
|
121
|
+
async function getGitHubLoginURL(redirect_uri, path) {
|
|
122
|
+
deleteSession();
|
|
123
|
+
if (!providers.github) {
|
|
124
|
+
throw new Error('GitHub provider not configured');
|
|
125
|
+
}
|
|
126
|
+
const { client_id } = providers.github;
|
|
127
|
+
return createGitHubOAuthLoginUrl(redirect_uri, path, client_id);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Completes OAuth flow by exchanging authorization code for a session.
|
|
274
131
|
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* }
|
|
280
|
-
* if (error?.message.includes('invalid_grant')) {
|
|
281
|
-
* return handleExpiredCode();
|
|
282
|
-
* }
|
|
283
|
-
* ```
|
|
132
|
+
* @param code Authorization code from OAuth provider
|
|
133
|
+
* @param redirect_uri OAuth redirect URI (must match login URL)
|
|
134
|
+
* @param state OAuth state containing provider information
|
|
135
|
+
* @returns Promise with error information
|
|
284
136
|
*/
|
|
285
|
-
async function
|
|
286
|
-
|
|
137
|
+
async function signInWithCode(code, redirect_uri, state = null) {
|
|
138
|
+
const provider = state ? JSON.parse(state).provider : null;
|
|
139
|
+
if (!provider) {
|
|
287
140
|
return {
|
|
288
|
-
error: new Error('
|
|
141
|
+
error: new Error('No provider specified in state')
|
|
289
142
|
};
|
|
290
143
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
144
|
+
let oauthToken = null;
|
|
145
|
+
if (providers.google && provider === 'google') {
|
|
146
|
+
const { client_id, client_secret } = providers.google;
|
|
147
|
+
const { data: googleData, error: exchangeError } = await exchangeCodeForGoogleIdToken(code, redirect_uri, client_id, client_secret, fetchImpl);
|
|
148
|
+
if (exchangeError) {
|
|
149
|
+
return {
|
|
150
|
+
error: exchangeError
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (!googleData) {
|
|
154
|
+
return {
|
|
155
|
+
error: new Error('No exchange data!')
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
oauthToken = googleData.id_token;
|
|
159
|
+
}
|
|
160
|
+
else if (providers.github && provider === 'github') {
|
|
161
|
+
const { client_id, client_secret } = providers.github;
|
|
162
|
+
const { data: githubData, error: exchangeError } = await exchangeCodeForGitHubIdToken(code, redirect_uri, client_id, client_secret, fetchImpl);
|
|
163
|
+
if (exchangeError) {
|
|
164
|
+
return {
|
|
165
|
+
error: exchangeError
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
if (!githubData) {
|
|
169
|
+
return {
|
|
170
|
+
error: new Error('No exchange data!')
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
oauthToken = githubData.access_token;
|
|
297
174
|
}
|
|
298
|
-
if (!
|
|
175
|
+
if (!oauthToken) {
|
|
299
176
|
return {
|
|
300
|
-
error: new Error('No
|
|
177
|
+
error: new Error('No OAuth token obtained')
|
|
301
178
|
};
|
|
302
179
|
}
|
|
303
|
-
const { data: signInData, error: signInError } = await auth.signInWithProvider(
|
|
180
|
+
const { data: signInData, error: signInError } = await auth.signInWithProvider(oauthToken, redirect_uri);
|
|
304
181
|
if (signInError) {
|
|
305
182
|
return {
|
|
306
|
-
|
|
307
|
-
error: signInError,
|
|
183
|
+
error: signInError
|
|
308
184
|
};
|
|
309
185
|
}
|
|
310
186
|
if (!signInData) {
|
|
311
187
|
return {
|
|
312
|
-
|
|
313
|
-
error: null,
|
|
188
|
+
error: null
|
|
314
189
|
};
|
|
315
190
|
}
|
|
316
191
|
const { data: sessionCookie, error: sessionError } = await adminAuth.createSessionCookie(signInData.idToken, {
|
|
317
|
-
expiresIn: 60 * 60 * 24 * 5 * 1000
|
|
192
|
+
expiresIn: 60 * 60 * 24 * 5 * 1000
|
|
318
193
|
});
|
|
319
194
|
if (sessionError) {
|
|
320
195
|
return {
|
|
321
|
-
error: sessionError
|
|
196
|
+
error: sessionError
|
|
322
197
|
};
|
|
323
198
|
}
|
|
324
199
|
if (!sessionCookie) {
|
|
325
200
|
return {
|
|
326
|
-
error: new Error('No session cookie returned')
|
|
201
|
+
error: new Error('No session cookie returned')
|
|
327
202
|
};
|
|
328
203
|
}
|
|
329
204
|
saveSession(sessionName, sessionCookie, COOKIE_OPTIONS);
|
|
330
205
|
return {
|
|
331
|
-
error: null
|
|
206
|
+
error: null
|
|
332
207
|
};
|
|
333
208
|
}
|
|
334
209
|
/**
|
|
335
|
-
* Generates
|
|
336
|
-
*
|
|
337
|
-
* This method creates new Firebase client tokens that can be used for authenticated
|
|
338
|
-
* requests to Firebase services from the client-side. The tokens are generated using
|
|
339
|
-
* the user's server-side session.
|
|
340
|
-
*
|
|
341
|
-
* @returns Promise resolving to token data (accessToken, refreshToken, etc.) and error information
|
|
342
|
-
*
|
|
343
|
-
* @example
|
|
344
|
-
* ```typescript
|
|
345
|
-
* // Using guard clauses for clean token handling
|
|
346
|
-
* const { data: tokenData, error } = await server.getToken();
|
|
347
|
-
* if (error) {
|
|
348
|
-
* console.error('Token generation failed:', error.message);
|
|
349
|
-
* return redirect('/login');
|
|
350
|
-
* }
|
|
351
|
-
*
|
|
352
|
-
* if (!tokenData) {
|
|
353
|
-
* console.log('No user session for token generation');
|
|
354
|
-
* return redirect('/login');
|
|
355
|
-
* }
|
|
210
|
+
* Generates fresh Firebase client tokens for the authenticated user.
|
|
356
211
|
*
|
|
357
|
-
*
|
|
358
|
-
* const response = {
|
|
359
|
-
* accessToken: tokenData.accessToken,
|
|
360
|
-
* refreshToken: tokenData.refreshToken,
|
|
361
|
-
* expiresIn: tokenData.expiresIn
|
|
362
|
-
* };
|
|
363
|
-
*
|
|
364
|
-
* return json(response);
|
|
365
|
-
*
|
|
366
|
-
* // Using tokens for Firebase client initialization
|
|
367
|
-
* const auth = getAuth(app);
|
|
368
|
-
* await signInWithCustomToken(auth, tokenData.accessToken);
|
|
369
|
-
*
|
|
370
|
-
* // Guard against token refresh failures
|
|
371
|
-
* if (tokenData.expiresIn < 300) { // Less than 5 minutes
|
|
372
|
-
* console.warn('Token expires soon, consider refreshing');
|
|
373
|
-
* }
|
|
374
|
-
* ```
|
|
212
|
+
* @returns Promise with token data and error
|
|
375
213
|
*/
|
|
376
214
|
async function getToken() {
|
|
377
215
|
const { data: verifiedToken, error: verifyError } = await getUser();
|
|
378
216
|
if (verifyError) {
|
|
379
217
|
return {
|
|
380
218
|
data: null,
|
|
381
|
-
error: verifyError
|
|
219
|
+
error: verifyError
|
|
382
220
|
};
|
|
383
221
|
}
|
|
384
222
|
if (!verifiedToken) {
|
|
385
223
|
return {
|
|
386
224
|
data: null,
|
|
387
|
-
error: null
|
|
225
|
+
error: null
|
|
388
226
|
};
|
|
389
227
|
}
|
|
390
228
|
const { data: signJWTData, error: signJWTError } = await signJWTCustomToken(verifiedToken.sub, serviceAccount);
|
|
391
229
|
if (signJWTError) {
|
|
392
230
|
return {
|
|
393
231
|
data: null,
|
|
394
|
-
error: signJWTError
|
|
232
|
+
error: signJWTError
|
|
395
233
|
};
|
|
396
234
|
}
|
|
397
235
|
if (!signJWTData) {
|
|
398
236
|
return {
|
|
399
237
|
data: null,
|
|
400
|
-
error: new Error('No custom token signed')
|
|
238
|
+
error: new Error('No custom token signed')
|
|
401
239
|
};
|
|
402
240
|
}
|
|
403
241
|
const { data: signInData, error: signInError } = await auth.signInWithCustomToken(signJWTData);
|
|
404
242
|
if (signInError) {
|
|
405
243
|
return {
|
|
406
244
|
data: null,
|
|
407
|
-
error: signInError
|
|
245
|
+
error: signInError
|
|
408
246
|
};
|
|
409
247
|
}
|
|
410
248
|
if (!signInData) {
|
|
411
249
|
return {
|
|
412
250
|
data: null,
|
|
413
|
-
error: null
|
|
251
|
+
error: null
|
|
414
252
|
};
|
|
415
253
|
}
|
|
416
254
|
return {
|
|
417
255
|
data: signInData,
|
|
418
|
-
error: null
|
|
256
|
+
error: null
|
|
419
257
|
};
|
|
420
258
|
}
|
|
421
|
-
/**
|
|
422
|
-
* Returns the complete Firebase Edge Server API with all authentication methods.
|
|
423
|
-
*
|
|
424
|
-
* @returns Object containing:
|
|
425
|
-
* - `auth`: Firebase Auth instance for client-side operations
|
|
426
|
-
* - `adminAuth`: Firebase Admin Auth instance for server-side operations
|
|
427
|
-
* - `signOut`: Function to sign out the current user
|
|
428
|
-
* - `getUser`: Function to get current user information with optional revocation checking
|
|
429
|
-
* - `getGoogleLoginURL`: Function to generate Google OAuth login URL
|
|
430
|
-
* - `signInWithGoogleWithCode`: Function to complete Google OAuth flow
|
|
431
|
-
* - `getToken`: Function to get fresh Firebase tokens for client-side usage
|
|
432
|
-
*/
|
|
433
259
|
return {
|
|
434
260
|
auth,
|
|
435
261
|
adminAuth,
|
|
436
262
|
signOut,
|
|
437
263
|
getUser,
|
|
438
264
|
getGoogleLoginURL,
|
|
439
|
-
|
|
440
|
-
|
|
265
|
+
getGitHubLoginURL,
|
|
266
|
+
signInWithCode,
|
|
267
|
+
getToken
|
|
441
268
|
};
|
|
442
269
|
}
|
|
443
270
|
//# sourceMappingURL=firebase-edge-server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"firebase-edge-server.js","sourceRoot":"","sources":["../src/firebase-edge-server.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAi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