Skip to content

Commit 780e016

Browse files
committed
refactor: rename api endpoint to __session
1 parent 998fe38 commit 780e016

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

packages/nuxt/src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const VueFire: NuxtModule<VueFireNuxtModuleOptions> =
108108

109109
if (nuxt.options.ssr) {
110110
addServerHandler({
111-
route: '/api/_vuefire/auth',
111+
route: '/api/__session',
112112
handler: resolve(runtimeDir, './auth/api.session'),
113113
})
114114
}

packages/nuxt/src/runtime/admin/plugin-auth-user.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
2929
if (isFirebaseError(err) && err.code === 'auth/id-token-expired') {
3030
// Other errors to be handled: auth/argument-error
3131
// the error is fine, the user is not logged in
32+
console.log('[VueFire]: Token expired -', err)
3233
} else {
3334
// ignore the error and consider the user as not logged in
34-
console.error('[VueFire]:', err)
35+
console.error('[VueFire]: Unknown Error -', err)
3536
}
3637
}
3738
}

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,37 @@ export default defineNuxtPlugin((nuxtApp) => {
1818
VueFireAuth(nuxtApp.payload.vuefireUser)(firebaseApp, nuxtApp.vueApp)
1919
const auth = getAuth(firebaseApp)
2020
// send a post request to the server when auth state changes to mint a cookie
21-
beforeAuthStateChanged(auth, mintCookie, () => {
22-
// rollback the auth state
23-
mintCookie(auth.currentUser)
24-
})
21+
beforeAuthStateChanged(
22+
auth,
23+
// if this fails, we rollback the auth state
24+
mintCookie,
25+
() => {
26+
// rollback the auth state
27+
mintCookie(auth.currentUser)
28+
}
29+
)
2530

2631
// we need both callback to avoid some race conditions
2732
onIdTokenChanged(auth, mintCookie)
2833
})
2934

35+
// TODO: should this be throttled to avoid multiple calls
3036
/**
3137
* Sends a post request to the server to mint a cookie based auth session. The name of the cookie is defined in the
3238
* api.session.ts file.
3339
*
3440
* @param user - the user to mint a cookie for
3541
*/
3642
async function mintCookie(user: User | null) {
37-
const jwtToken = await user?.getIdToken()
43+
const jwtToken = await user?.getIdToken(/* forceRefresh */ true)
3844
// throws if the server returns an error so that beforeAuthStateChanged can catch it to cancel
39-
await $fetch('/api/_vuefire/auth', {
40-
method: 'POST',
41-
// if the token is undefined, the server will delete the cookie
42-
body: { token: jwtToken },
43-
})
45+
await $fetch(
46+
// '/api/__session-server',
47+
'/api/__session',
48+
{
49+
method: 'POST',
50+
// if the token is undefined, the server will delete the cookie
51+
body: { token: jwtToken },
52+
}
53+
)
4454
}

0 commit comments

Comments
 (0)