Skip to content

Commit

Permalink
fix: avoid auto-logout on SSR error
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Mar 9, 2024
1 parent 309f48d commit 921662e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/runtime/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useAuthToken } from '../composables/useAuthToken'
import {
defineNuxtRouteMiddleware,
useRuntimeConfig,
navigateTo
navigateTo,
useNuxtApp
} from '#imports'

export default defineNuxtRouteMiddleware((to) => {
Expand All @@ -15,7 +16,10 @@ export default defineNuxtRouteMiddleware((to) => {
return
}

if (publicConfig.enableGlobalAuthMiddleware && to.meta.auth === false) {
// Makes sure no infinite redirections on ssr error
const isSSRError = process.server && typeof useNuxtApp().payload.error !== 'undefined'

if (isSSRError || (publicConfig.enableGlobalAuthMiddleware && to.meta.auth === false)) {
return
}

Expand Down
3 changes: 2 additions & 1 deletion src/runtime/plugins/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
/**
* Makes sure to refresh access token and set user state if possible (run once)
*/
const isSSRError = typeof nuxtApp.payload.error !== 'undefined'
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 && !isSSRError) || (process.client && (!isServerRendered || isPrerenderd || isSSRError))

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

0 comments on commit 921662e

Please sign in to comment.