Skip to content

Commit 509bd60

Browse files
committed
perf(module): do not call api on every visit for session validation
1 parent d7c03d0 commit 509bd60

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

src/module/src/runtime/server/routes/auth/github.get.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ export default eventHandler(async (event: H3Event) => {
215215

216216
const redirect = decodeURIComponent(getCookie(event, 'studio-redirect') || '')
217217
deleteCookie(event, 'studio-redirect')
218+
219+
// Set a cookie to indicate that the session is active
220+
setCookie(event, 'studio-session-check', 'true', { httpOnly: false })
221+
218222
// make sure the redirect is a valid relative path (avoid also // which is not a valid URL)
219223
if (redirect && redirect.startsWith('/') && !redirect.startsWith('//')) {
220224
return sendRedirect(event, redirect)

src/module/src/runtime/server/routes/auth/session.delete.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { eventHandler, useSession } from 'h3'
1+
import { eventHandler, useSession, deleteCookie } from 'h3'
22
import { useRuntimeConfig } from '#imports'
33

44
export default eventHandler(async (event) => {
@@ -9,5 +9,8 @@ export default eventHandler(async (event) => {
99

1010
await session.clear()
1111

12+
// Delete the cookie to indicate that the session is inactive
13+
deleteCookie(event, 'studio-session-check')
14+
1215
return { loggedOut: true }
1316
})

src/module/src/runtime/server/routes/auth/session.get.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { eventHandler, useSession } from 'h3'
1+
import { eventHandler, useSession, deleteCookie } from 'h3'
22
import { useRuntimeConfig } from '#imports'
33

44
export default eventHandler(async (event) => {
@@ -7,6 +7,11 @@ export default eventHandler(async (event) => {
77
password: useRuntimeConfig(event).studio?.auth?.sessionSecret,
88
})
99

10+
if (!session.data) {
11+
// Delete the cookie to indicate that the session is inactive
12+
deleteCookie(event, 'studio-session-check')
13+
}
14+
1015
return {
1116
...session.data,
1217
id: session.id!,

src/module/src/runtime/utils/activation.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { getAppManifest, useState, useRuntimeConfig } from '#imports'
1+
import { getAppManifest, useState, useRuntimeConfig, useCookie } from '#imports'
22
import type { StudioUser } from 'nuxt-studio/app'
33

44
export async function defineStudioActivationPlugin(onStudioActivation: (user: StudioUser) => Promise<void>) {
55
const user = useState<StudioUser | null>('studio-session', () => null)
66
const config = useRuntimeConfig().public.studio
7+
const cookie = useCookie('studio-session-check')
78

89
if (config.dev) {
910
return await onStudioActivation({
@@ -16,9 +17,9 @@ export async function defineStudioActivationPlugin(onStudioActivation: (user: St
1617
})
1718
}
1819

19-
await $fetch<{ user: StudioUser }>('/__nuxt_studio/auth/session').then((session) => {
20-
user.value = session?.user ?? null
21-
})
20+
user.value = String(cookie.value) === 'true'
21+
? await $fetch<{ user: StudioUser }>('/__nuxt_studio/auth/session').then(session => session?.user ?? null)
22+
: null
2223

2324
let mounted = false
2425
if (user.value?.email) {
@@ -36,7 +37,9 @@ export async function defineStudioActivationPlugin(onStudioActivation: (user: St
3637
// Listen to CMD + . to toggle the studio or redirect to the login page
3738
document.addEventListener('keydown', (event) => {
3839
if (event.metaKey && event.key === '.') {
39-
window.location.href = config.route + '?redirect=' + encodeURIComponent(window.location.pathname)
40+
setTimeout(() => {
41+
window.location.href = config.route + '?redirect=' + encodeURIComponent(window.location.pathname)
42+
})
4043
}
4144
})
4245
}

0 commit comments

Comments
 (0)