fix: auth redirects and routing context issues
This commit is contained in:
@@ -45,7 +45,7 @@ const { isAuthenticated, user, logout } = useAuth()
|
|||||||
<div class="header-slim-right-zone" role="navigation">
|
<div class="header-slim-right-zone" role="navigation">
|
||||||
<!-- Utente Autenticato: Bottone Utente + Bottone Esci -->
|
<!-- Utente Autenticato: Bottone Utente + Bottone Esci -->
|
||||||
<div v-if="isAuthenticated" class="d-flex align-items-center gap-2">
|
<div v-if="isAuthenticated" class="d-flex align-items-center gap-2">
|
||||||
<NuxtLink class="btn btn-primary btn-icon btn-full" to="/dashboard"
|
<NuxtLink class="btn btn-primary btn-icon btn-full" to="/me"
|
||||||
data-element="personal-area-dashboard" data-focus-mouse="false">
|
data-element="personal-area-dashboard" data-focus-mouse="false">
|
||||||
<span class="rounded-icon" aria-hidden="true">
|
<span class="rounded-icon" aria-hidden="true">
|
||||||
<BIIcon name="it-user" />
|
<BIIcon name="it-user" />
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ import { defu } from 'defu'
|
|||||||
|
|
||||||
export const useApiFetch = <T>(request: string, options: Parameters<typeof $fetch>[1] = {}) => {
|
export const useApiFetch = <T>(request: string, options: Parameters<typeof $fetch>[1] = {}) => {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const tokenCookie = useCookie('auth_token')
|
const nuxtApp = useNuxtApp()
|
||||||
|
const tokenCookie = useCookie('auth_token', { path: '/' })
|
||||||
const tokenState = useState<string | null>('auth_token_state')
|
const tokenState = useState<string | null>('auth_token_state')
|
||||||
|
|
||||||
const tokenValue = tokenState.value ?? tokenCookie.value
|
const tokenValue = tokenState.value ?? tokenCookie.value
|
||||||
@@ -38,7 +39,7 @@ export const useApiFetch = <T>(request: string, options: Parameters<typeof $fetc
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
navigateTo('/auth/login')
|
nuxtApp.runWithContext(() => navigateTo('/auth/login'))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[API] Error navigating to login:', e)
|
console.error('[API] Error navigating to login:', e)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,13 @@ import type {
|
|||||||
} from "~/types/api";
|
} from "~/types/api";
|
||||||
|
|
||||||
export const useAuth = () => {
|
export const useAuth = () => {
|
||||||
const token = useCookie('auth_token', {
|
const route = useRoute()
|
||||||
|
const nuxtApp = useNuxtApp()
|
||||||
|
|
||||||
|
const token = useCookie<string | null>('auth_token', {
|
||||||
maxAge: 60 * 60 * 12,
|
maxAge: 60 * 60 * 12,
|
||||||
sameSite: 'lax',
|
sameSite: 'lax',
|
||||||
|
path: '/',
|
||||||
watch: true
|
watch: true
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -25,7 +29,7 @@ export const useAuth = () => {
|
|||||||
tokenState.value = newVal || null
|
tokenState.value = newVal || null
|
||||||
})
|
})
|
||||||
|
|
||||||
const syncUser = async () => {
|
const syncUser = async (redirectOnSuccess = false) => {
|
||||||
const tokenVal = tokenState.value || token.value
|
const tokenVal = tokenState.value || token.value
|
||||||
if (!tokenVal || isSyncingUser.value) return
|
if (!tokenVal || isSyncingUser.value) return
|
||||||
|
|
||||||
@@ -36,12 +40,11 @@ export const useAuth = () => {
|
|||||||
console.log('[Auth] User synced successfully')
|
console.log('[Auth] User synced successfully')
|
||||||
user.value = res.user
|
user.value = res.user
|
||||||
|
|
||||||
const route = useRoute()
|
|
||||||
if (res.user.must_change_password && route.path !== '/auth/change-password') {
|
if (res.user.must_change_password && route.path !== '/auth/change-password') {
|
||||||
console.log('[Auth] User must change password, redirecting...')
|
console.log('[Auth] User must change password, redirecting...')
|
||||||
await navigateTo('/auth/change-password')
|
await nuxtApp.runWithContext(() => navigateTo('/auth/change-password'))
|
||||||
} else {
|
} else if (redirectOnSuccess && route.path.startsWith('/auth')) {
|
||||||
await navigateTo('/dashboard')
|
await nuxtApp.runWithContext(() => navigateTo('/me'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -55,14 +58,14 @@ export const useAuth = () => {
|
|||||||
const handleLoginSuccess = async (jwtToken: string) => {
|
const handleLoginSuccess = async (jwtToken: string) => {
|
||||||
token.value = jwtToken
|
token.value = jwtToken
|
||||||
tokenState.value = jwtToken
|
tokenState.value = jwtToken
|
||||||
await syncUser()
|
await syncUser(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
token.value = null
|
token.value = null
|
||||||
tokenState.value = null
|
tokenState.value = null
|
||||||
user.value = null
|
user.value = null
|
||||||
navigateTo('/auth/login')
|
nuxtApp.runWithContext(() => navigateTo('/auth/login'))
|
||||||
}
|
}
|
||||||
|
|
||||||
const register = (body: RegisterCitizenRequest) =>
|
const register = (body: RegisterCitizenRequest) =>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
export default defineNuxtRouteMiddleware(async () => {
|
export default defineNuxtRouteMiddleware(async () => {
|
||||||
const auth = useAuth()
|
const auth = useAuth()
|
||||||
|
|
||||||
// Se c'è un token ma l'utente non è ancora caricato e non stiamo già sincronizzando, sincronizza
|
|
||||||
if ((auth.token.value || auth.tokenState.value) && !auth.user.value && !auth.isSyncingUser.value) {
|
if ((auth.token.value || auth.tokenState.value) && !auth.user.value && !auth.isSyncingUser.value) {
|
||||||
try {
|
try {
|
||||||
await auth.syncUser()
|
await auth.syncUser()
|
||||||
@@ -10,8 +9,7 @@ export default defineNuxtRouteMiddleware(async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Se autenticato, redirige al dashboard
|
|
||||||
if (auth.isAuthenticated.value && auth.user.value) {
|
if (auth.isAuthenticated.value && auth.user.value) {
|
||||||
return navigateTo('/dashboard')
|
return navigateTo('/me')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -45,7 +45,7 @@ const handleSubmit = async () => {
|
|||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
await syncUser()
|
await syncUser()
|
||||||
await router.push('/dashboard')
|
await router.push('/me')
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
errorMessage.value = error.data?.message || 'Errore. Verifica la password attuale.'
|
errorMessage.value = error.data?.message || 'Errore. Verifica la password attuale.'
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ const handleVerifyOtp = async () => {
|
|||||||
try {
|
try {
|
||||||
const res = await verifyOtp({ email: email.value, code: otpCode.value })
|
const res = await verifyOtp({ email: email.value, code: otpCode.value })
|
||||||
await handleLoginSuccess(res.token)
|
await handleLoginSuccess(res.token)
|
||||||
router.push('/dashboard')
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
errorMessage.value = 'Codice OTP non valido o scaduto.'
|
errorMessage.value = 'Codice OTP non valido o scaduto.'
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user