Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Nov 14, 2024
1 parent 352ad46 commit 4822dde
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions playground/auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
21 changes: 11 additions & 10 deletions playground/middleware/jtw.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }) {
Expand All @@ -53,7 +53,8 @@ export default defineNuxtRouteMiddleware(async (route) => {
}
},
})
await fetch()
// refresh the session
await fetchSession()
}
})

Expand Down
1 change: 0 additions & 1 deletion playground/server/api/jtw/create.post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { randomUUID } from 'node:crypto'
import jwt from '@tsndr/cloudflare-worker-jwt'

export default defineEventHandler(async (event) => {
Expand Down

0 comments on commit 4822dde

Please sign in to comment.