-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from wisemen-digital/fix/middleware_infinite_re…
…direct fix: fixed the infinite redirect in middleware, cant use useLocalePat…
- Loading branch information
Showing
1 changed file
with
11 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) |