Skip to content

Commit

Permalink
Merge pull request #7 from wisemen-digital/fix/middleware_infinite_re…
Browse files Browse the repository at this point in the history
…direct

fix: fixed the infinite redirect in middleware, cant use useLocalePat…
  • Loading branch information
Robbe95 authored Jun 25, 2024
2 parents d0eacfb + 46714ba commit d7bfff1
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions layers/auth/middleware/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { useAuthStore } from '@auth/stores/auth.store'
import { useGlobalI18n } from '@base/composables/i18n/useGlobaI18n'
import { defineNuxtRouteMiddleware } from 'nuxt/app'

export default defineNuxtRouteMiddleware(async () => {
const authStore = useAuthStore()
const { locale } = useGlobalI18n()
const oAuthClient = useNuxtApp().$oAuthClient
const localePath = `/${locale.value}`

const hasTokens = oAuthClient.isLoggedIn()

const localePath = useLocalePath()

if (hasTokens == null) {
return navigateTo(localePath('/auth/login'))
}

const loginRedirectPath = `${localePath}/auth/login`
let userFound = true

try {
const hasTokens = oAuthClient.isLoggedIn()

if (hasTokens == null) {
return navigateTo(loginRedirectPath)
}

await authStore.getCurrentUser()
}
catch (error) {
userFound = false
}

if (!userFound) {
return navigateTo(localePath('/auth/login'))
return navigateTo(loginRedirectPath)
}
})

0 comments on commit d7bfff1

Please sign in to comment.