Skip to content

Commit 617edfe

Browse files
committed
fix(auth): authenticated requests on server
Fix #1310
1 parent 04795b0 commit 617edfe

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

packages/nuxt/src/runtime/app/plugin.server.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { deleteApp, FirebaseApp, initializeApp } from 'firebase/app'
2-
import type { User } from 'firebase/auth'
1+
import { deleteApp, type FirebaseApp, initializeApp } from 'firebase/app'
2+
import { getAuth, signInWithCustomToken, type User } from 'firebase/auth'
3+
import { type App as AdminApp } from 'firebase-admin/app'
4+
import { getAuth as getAdminAuth } from 'firebase-admin/auth'
35
import { LRUCache } from 'lru-cache'
46
import { log } from '../logging'
57
import { UserSymbol } from '../constants'
@@ -23,7 +25,7 @@ const appCache = new LRUCache<string, FirebaseApp>({
2325
/**
2426
* Initializes the app and provides it to others.
2527
*/
26-
export default defineNuxtPlugin((nuxtApp) => {
28+
export default defineNuxtPlugin(async (nuxtApp) => {
2729
const appConfig = useAppConfig()
2830

2931
const user = nuxtApp[
@@ -32,22 +34,36 @@ export default defineNuxtPlugin((nuxtApp) => {
3234
] as User | undefined | null
3335
const uid = user?.uid
3436

35-
let firebaseApp: FirebaseApp
37+
let firebaseApp: FirebaseApp | undefined
3638

3739
// log('debug', 'initializing app with', appConfig.firebaseConfig)
3840
if (uid) {
39-
if (!appCache.has(uid)) {
41+
firebaseApp = appCache.get(uid)
42+
if (!firebaseApp) {
4043
const randomId = Math.random().toString(36).slice(2)
44+
// TODO: do we need a randomId?
4145
const appName = `auth:${user.uid}:${randomId}`
4246

43-
// log('debug', ' creating new app', appName)
47+
log('log', '👤 creating new app', appName)
4448

4549
appCache.set(uid, initializeApp(appConfig.firebaseConfig, appName))
50+
firebaseApp = appCache.get(uid)!
51+
const firebaseAdminApp = nuxtApp.$firebaseAdminApp as AdminApp
52+
const adminAuth = getAdminAuth(firebaseAdminApp)
53+
// console.time('token')
54+
const customToken = await adminAuth.createCustomToken(user.uid)
55+
// console.timeLog('token', `got token for ${user.uid}`)
56+
const credentials = await signInWithCustomToken(
57+
getAuth(firebaseApp),
58+
customToken
59+
)
60+
// console.timeLog('token', `signed in with token for ${user.uid}`)
61+
// console.timeEnd('token')
62+
// TODO: token expiration (1h)
4663
}
47-
firebaseApp = appCache.get(uid)!
4864
} else {
4965
// anonymous session, just create a new app
50-
// log('debug', 'anonymous session')
66+
// log('log', '🥸 anonymous session')
5167
firebaseApp = initializeApp(appConfig.firebaseConfig)
5268
}
5369

packages/nuxt/src/runtime/auth/plugin.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { VueFireAuth } from 'vuefire'
33
import { defineNuxtPlugin } from '#app'
44

55
/**
6-
* Setups VueFireAuth for the client.
6+
* Setups VueFireAuth for the client. This version creates some listeners that shouldn't be set on server.
77
*/
88
export default defineNuxtPlugin((nuxtApp) => {
99
const firebaseApp = nuxtApp.$firebaseApp as FirebaseApp

packages/nuxt/src/runtime/auth/plugin.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { FirebaseApp } from 'firebase/app'
22
import type { User } from 'firebase/auth'
33
import { VueFireAuthServer } from 'vuefire/server'
44
import type { App } from 'vue'
5-
import { defineNuxtPlugin } from '#app'
65
import { UserSymbol } from '../constants'
6+
import { defineNuxtPlugin } from '#app'
77

88
/**
99
* Setups the auth state based on the cookie.

0 commit comments

Comments
 (0)