From 4822dde17be534b4751c52c3f7aa2ae70bff9079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Thu, 14 Nov 2024 11:15:23 +0100 Subject: [PATCH] chore: update --- playground/auth.d.ts | 8 ++++---- playground/middleware/jtw.global.ts | 21 +++++++++++---------- playground/server/api/jtw/create.post.ts | 1 - 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/playground/auth.d.ts b/playground/auth.d.ts index 7c01cd6..556aa0b 100644 --- a/playground/auth.d.ts +++ b/playground/auth.d.ts @@ -29,15 +29,15 @@ declare module '#auth-utils' { polar?: string zitadel?: string authentik?: string - jtw?: { - accessToken: string - refreshToken: string - } } interface UserSession { // eslint-disable-next-line @typescript-eslint/no-explicit-any extended?: any + jtw?: { + accessToken: string + refreshToken: string + } loggedInAt: number } diff --git a/playground/middleware/jtw.global.ts b/playground/middleware/jtw.global.ts index 3d5759c..39498ee 100644 --- a/playground/middleware/jtw.global.ts +++ b/playground/middleware/jtw.global.ts @@ -3,31 +3,31 @@ import { parse, parseSetCookie, serialize } from 'cookie-es' import type { JwtData } from '@tsndr/cloudflare-worker-jwt' import { decode } from '@tsndr/cloudflare-worker-jwt' -export default defineNuxtRouteMiddleware(async (route) => { +export default defineNuxtRouteMiddleware(async () => { const nuxtApp = useNuxtApp() // Don't run on client hydration when server rendered if (import.meta.client && nuxtApp.isHydrating && nuxtApp.payload.serverRendered) return + const { session, clear: clearSession, fetch: fetchSession } = useUserSession() + // Ignore if no tokens + if (!session.value?.jwt) return + const serverEvent = useRequestEvent() const runtimeConfig = useRuntimeConfig() - const { session, clear, fetch } = useUserSession() - const { accessToken, refreshToken } = session.value?.jwt || {} - // Ignore if no tokens - if (!accessToken || !refreshToken) return + const { accessToken, refreshToken } = session.value.jwt const accessPayload = decode(accessToken) const refreshPayload = decode(refreshToken) - // console.log(accessPayload, '\n', refreshPayload) // Both tokens expired, clearing session if (isExpired(accessPayload) && isExpired(refreshPayload)) { - console.log('both tokens expired, clearing session') - await clear() + console.info('both tokens expired, clearing session') + await clearSession() // return navigateTo('/login') } // Access token expired, refreshing else if (isExpired(accessPayload)) { - console.log('access token expired, refreshing') + console.info('access token expired, refreshing') await useRequestFetch()('/api/jtw/refresh', { method: 'POST', onResponse({ response: { headers } }) { @@ -53,7 +53,8 @@ export default defineNuxtRouteMiddleware(async (route) => { } }, }) - await fetch() + // refresh the session + await fetchSession() } }) diff --git a/playground/server/api/jtw/create.post.ts b/playground/server/api/jtw/create.post.ts index 0d448b1..4cc2446 100644 --- a/playground/server/api/jtw/create.post.ts +++ b/playground/server/api/jtw/create.post.ts @@ -1,4 +1,3 @@ -import { randomUUID } from 'node:crypto' import jwt from '@tsndr/cloudflare-worker-jwt' export default defineEventHandler(async (event) => {