Skip to content

Commit

Permalink
fix(auth): avoid auto-logout when page not found (#79)
Browse files Browse the repository at this point in the history
* skip refresh logic if page not found

* avoid infinite redirection loop when page not found
  • Loading branch information
becem-gharbi authored Mar 11, 2024
1 parent c9fa3c7 commit d8e03bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/runtime/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export default defineNuxtRouteMiddleware((to) => {
return
}

const isPageFound = to.matched.length > 0

if (!isPageFound && process.server) {
return
}

if (config.auth.enableGlobalAuthMiddleware === true) {
if (to.meta.auth === false) {
return
Expand Down
8 changes: 5 additions & 3 deletions src/runtime/plugins/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
addRouteMiddleware,
useRuntimeConfig,
useDirectusAuth,
useRoute,
useDirectusSession
useDirectusSession,
useRouter
} from '#imports'

export default defineNuxtPlugin(async (nuxtApp) => {
try {
const config = useRuntimeConfig().public.directus as PublicConfig & { auth: { enabled: true } }
const router = useRouter()

addRouteMiddleware('common', common, { global: true })
addRouteMiddleware('auth', auth, { global: config.auth.enableGlobalAuthMiddleware })
Expand All @@ -23,9 +24,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
const { _loggedInFlag } = useDirectusSession()
const token = useDirectusToken()

const isPageFound = router.currentRoute.value?.matched.length > 0
const isPrerenderd = typeof nuxtApp.payload.prerenderedAt === 'number'
const isServerRendered = nuxtApp.payload.serverRendered
const firstTime = (process.server && !isPrerenderd) || (process.client && (!isServerRendered || isPrerenderd))
const firstTime = (process.server && !isPrerenderd && isPageFound) || (process.client && (!isServerRendered || isPrerenderd || !isPageFound))

if (firstTime) {
const isCallback = useRoute().path === config.auth.redirect.callback
Expand Down

0 comments on commit d8e03bb

Please sign in to comment.