Skip to content

Commit 9af5c99

Browse files
committed
feat(auth): allow turning off session cookie
1 parent 82ce282 commit 9af5c99

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

packages/nuxt/playground/nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export default defineNuxtConfig({
3939
[
4040
'../src/module',
4141
{
42-
auth: true,
42+
auth: {
43+
enabled: true,
44+
sessionCookie: true,
45+
},
4346
appCheck: {
4447
// TODO: could automatically pick up a debug token defined as an env variable
4548
debug: process.env.NODE_ENV !== 'production',

packages/nuxt/src/module.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,34 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
5959
nuxt.options.appConfig.firebaseConfig = markRaw(options.config)
6060
nuxt.options.appConfig.vuefireOptions = markRaw(options)
6161

62-
nuxt.options.runtimeConfig.vuefire = {
63-
options: {
64-
...options,
65-
// ensure the resolved version easier to consume
66-
emulators: {
67-
enabled:
68-
typeof options.emulators === 'object'
69-
? options.emulators.enabled ?? true // allows user to comment out enabled: false
70-
: !!options.emulators,
71-
...(typeof options.emulators === 'object' ? options.emulators : {}),
72-
},
62+
const isAuthEnabled =
63+
typeof options.auth === 'object'
64+
? options.auth.enabled ?? true // allows user to comment out enabled: false
65+
: !!options.auth
66+
67+
const resolvedVueFireOptions = {
68+
...options,
69+
// ensure the resolved version easier to consume
70+
emulators: {
71+
enabled:
72+
typeof options.emulators === 'object'
73+
? options.emulators.enabled ?? true // allows user to comment out enabled: false
74+
: !!options.emulators,
75+
...(typeof options.emulators === 'object' ? options.emulators : {}),
7376
},
77+
auth: {
78+
enabled: isAuthEnabled,
79+
// enable session cookie when auth is `true`
80+
sessionCookie:
81+
typeof options.auth === 'object'
82+
? isAuthEnabled && options.auth.sessionCookie // deactivating auth also deactivates the session cookie
83+
: !!options.auth, // fallback to the boolean value of options.auth
84+
...(typeof options.auth === 'object' ? options.auth : {}),
85+
},
86+
} satisfies VueFireNuxtModuleOptionsResolved
87+
88+
nuxt.options.runtimeConfig.vuefire = {
89+
options: resolvedVueFireOptions,
7490
}
7591

7692
// we need this to avoid some warnings about missing credentials and ssr
@@ -169,7 +185,11 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
169185
)
170186
}
171187

172-
if (nuxt.options.ssr && (hasServiceAccount || emulatorsConfig)) {
188+
if (
189+
nuxt.options.ssr &&
190+
(hasServiceAccount || emulatorsConfig) &&
191+
resolvedVueFireOptions.auth.sessionCookie
192+
) {
173193
// Add the session handler than mints a cookie for the user
174194
addServerHandler({
175195
route: '/api/__session',
@@ -243,7 +263,7 @@ export default defineNuxtModule<VueFireNuxtModuleOptions>({
243263
}
244264

245265
if (hasServiceAccount || emulatorsConfig) {
246-
if (options.auth) {
266+
if (resolvedVueFireOptions.auth.sessionCookie) {
247267
// decodes user token from cookie if any
248268
addPlugin(resolve(runtimeDir, 'auth/plugin-user-token.server'))
249269
}

packages/nuxt/src/module/options.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,34 @@ export interface VueFireNuxtModuleOptions {
3939
appCheck?: NuxtVueFireAppCheckOptions
4040

4141
/**
42-
* Enables Authentication
42+
* Enables the Authentication module and the session cookie. Pass an object to individually customize the modules.
43+
* @defaultValue `false`
4344
*/
44-
auth?: boolean
45+
auth?:
46+
| boolean
47+
| {
48+
/**
49+
* Adds the Authentication module to VueFire.
50+
* @defaultValue `true` if `options.auth` is an object.
51+
*/
52+
enabled?: boolean
53+
54+
/**
55+
* Enables the `/api/__session` endpoint to mint cookies and verifying the user during SSR. This requires you to
56+
* configure a [valid Service
57+
* Account](https://vuefire.vuejs.org/nuxt/getting-started.html#Configuring-the-Admin-SDK) and the valid
58+
* permissions on your Google Cloud project. You can find more information about what happens behind the scenes
59+
* in Firebase docs: [Manage Session Cookies](https://firebase.google.com/docs/auth/admin/manage-cookies).
60+
*/
61+
sessionCookie?: boolean
62+
}
4563

4664
/**
4765
* Controls whether to use emulators or not. Pass `false` to disable emulators. When set to `true`, emulators are
4866
* enabled when they are detected in the `firebase.json` file. You still need to run the emulators in parallel to your
4967
* app.
5068
*
51-
* @default true
69+
* @defaultValue `true`
5270
* @experimental
5371
*/
5472
emulators?:
@@ -76,6 +94,7 @@ export interface VueFireNuxtModuleOptions {
7694
}
7795

7896
export interface VueFireNuxtModuleOptionsResolved
79-
extends Omit<VueFireNuxtModuleOptions, 'emulators'> {
97+
extends Omit<VueFireNuxtModuleOptions, 'emulators' | 'auth'> {
8098
emulators: Exclude<VueFireNuxtModuleOptions['emulators'], boolean | undefined>
99+
auth: Exclude<VueFireNuxtModuleOptions['auth'], boolean | undefined>
81100
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
2424

2525
const uid = decodedToken?.uid
2626

27+
// this is also undefined if the user hasn't enabled the session cookie option
2728
if (uid) {
2829
// reauthenticate if the user is not the same (e.g. invalidated)
2930
if (auth.currentUser?.uid !== uid) {

0 commit comments

Comments
 (0)