@innertia-solutions/innertia-nuxt 0.1.69 → 0.1.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/composables/useApi.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// useRequestInterceptors auto-imported from nuxt-core
|
|
2
|
+
import { deleteCookie } from 'h3'
|
|
2
3
|
// useAuthStore auto-imported from this package
|
|
3
4
|
|
|
4
5
|
export function useApi() {
|
|
@@ -9,6 +10,10 @@ export function useApi() {
|
|
|
9
10
|
? (config.apiInternalUrl || config.public.apiBaseUrl || '/api')
|
|
10
11
|
: (config.public.apiBaseUrl || '/api')
|
|
11
12
|
const loginPath = config.public.loginPath || '/login'
|
|
13
|
+
// Capturados en setup (contexto Nuxt síncrono): tras un await en makeRequest
|
|
14
|
+
// el contexto puede no estar disponible.
|
|
15
|
+
const requestEvent = import.meta.server ? useRequestEvent() : null
|
|
16
|
+
const router = import.meta.client ? useRouter() : null
|
|
12
17
|
|
|
13
18
|
const { run, add } = useRequestInterceptors()
|
|
14
19
|
|
|
@@ -66,7 +71,19 @@ export function useApi() {
|
|
|
66
71
|
if (response.status === 401) {
|
|
67
72
|
const authStore = useAuthStore()
|
|
68
73
|
authStore.logout()
|
|
69
|
-
|
|
74
|
+
// La sesión sellada quedó inválida (token revocado, usuario borrado, BD
|
|
75
|
+
// reseteada): hay que BORRARLA. Si no, cada request vuelve "autenticado",
|
|
76
|
+
// guest manda a home, auth/me da 401 y se redirige a login → loop infinito
|
|
77
|
+
// (ERR_TOO_MANY_REDIRECTS).
|
|
78
|
+
if (requestEvent) {
|
|
79
|
+
requestEvent.context.authToken = undefined
|
|
80
|
+
deleteCookie(requestEvent, 'innertia_session', { path: '/' })
|
|
81
|
+
} else if (import.meta.client) {
|
|
82
|
+
try { await $fetch('/_auth/logout', { method: 'POST' }) } catch { /* best-effort */ }
|
|
83
|
+
}
|
|
84
|
+
const current = requestEvent ? requestEvent.path?.split('?')[0] : router?.currentRoute.value.path
|
|
85
|
+
// Ya en una página de login (la del producto o /platform/login): no redirigir.
|
|
86
|
+
if (current !== loginPath && !current?.endsWith('/login')) await navigateTo(loginPath)
|
|
70
87
|
return null
|
|
71
88
|
}
|
|
72
89
|
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defineEventHandler, proxyRequest, getRequestURL } from 'h3'
|
|
2
|
-
import { readSession } from '../../utils/session'
|
|
2
|
+
import { readSession, clearSession } from '../../utils/session'
|
|
3
3
|
import { resolveApiTarget, buildAuthHeaders } from '../../utils/bff'
|
|
4
4
|
|
|
5
5
|
// BFF: reenvía /api/** a Laravel inyectando el Bearer desde la sesión sellada.
|
|
@@ -11,5 +11,12 @@ export default defineEventHandler(async (event) => {
|
|
|
11
11
|
const path = (event.context.params?.path ?? '') as string
|
|
12
12
|
const search = getRequestURL(event).search
|
|
13
13
|
const target = resolveApiTarget(config.apiInternalUrl, path, search)
|
|
14
|
-
return proxyRequest(event, target, {
|
|
14
|
+
return proxyRequest(event, target, {
|
|
15
|
+
headers: buildAuthHeaders(token),
|
|
16
|
+
// Token rechazado por Laravel → la sesión sellada no sirve: borrarla para que
|
|
17
|
+
// /_auth/session deje de reportar authenticated (evita loops de redirección).
|
|
18
|
+
onResponse: (ev, response) => {
|
|
19
|
+
if (token && response.status === 401) clearSession(ev)
|
|
20
|
+
},
|
|
21
|
+
})
|
|
15
22
|
})
|