@innertia-solutions/innertia-nuxt 0.1.71 → 0.1.73
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/useAuth.js +27 -1
- package/package.json +1 -1
- package/server/routes/_auth/login.post.ts +2 -1
- package/server/routes/_auth/otp/verify.post.ts +8 -0
- package/server/routes/_auth/password/change.post.ts +9 -0
- package/server/routes/_auth/platform/login.post.ts +1 -1
- package/server/utils/__tests__/login.test.ts +9 -0
- package/server/utils/authStep.ts +31 -0
- package/server/utils/login.ts +18 -8
package/composables/useAuth.js
CHANGED
|
@@ -19,6 +19,32 @@ export function useAuth() {
|
|
|
19
19
|
method: 'POST',
|
|
20
20
|
body: { context, email, password, remember },
|
|
21
21
|
})
|
|
22
|
+
// Paso previo (cambio forzado / OTP / 2FA): sin sesión todavía → el caller
|
|
23
|
+
// decide (p. ej. pantalla de cambio de clave con data.user_id).
|
|
24
|
+
if (!data?.user && (data?.requires_password_change || data?.requires_otp || data?.requires_2fa || data?.requires_email_verification)) {
|
|
25
|
+
return data
|
|
26
|
+
}
|
|
27
|
+
return finishLogin(context, data)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Paso previo verificado: verificar código OTP del correo (sella la sesión). */
|
|
31
|
+
async function verifyOtp(context, userId, code) {
|
|
32
|
+
const data = await $fetch('/_auth/otp/verify', { method: 'POST', body: { context, user_id: userId, code } })
|
|
33
|
+
if (!data?.user) return data // siguiente paso (p. ej. requires_2fa)
|
|
34
|
+
return finishLogin(context, data)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Cambio de clave forzado pre-auth (con código OTP si se exige); sella la sesión. */
|
|
38
|
+
async function changeForcedPassword(context, userId, { password, passwordConfirmation, code } = {}) {
|
|
39
|
+
const data = await $fetch('/_auth/password/change', {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
body: { context, user_id: userId, code, password, password_confirmation: passwordConfirmation },
|
|
42
|
+
})
|
|
43
|
+
if (!data?.user) return data
|
|
44
|
+
return finishLogin(context, data)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function finishLogin(context, data) {
|
|
22
48
|
authStore.setAuthenticated(true)
|
|
23
49
|
if (data.user) authStore.saveUser(data.user)
|
|
24
50
|
authStore.setCurrentContext(context)
|
|
@@ -94,5 +120,5 @@ export function useAuth() {
|
|
|
94
120
|
return data.url ?? data
|
|
95
121
|
}
|
|
96
122
|
|
|
97
|
-
return { performLogin, fetchMe, logout, getOauthRedirectUrl }
|
|
123
|
+
return { performLogin, verifyOtp, changeForcedPassword, fetchMe, logout, getOauthRedirectUrl }
|
|
98
124
|
}
|
package/package.json
CHANGED
|
@@ -21,6 +21,7 @@ export default defineEventHandler(async (event) => {
|
|
|
21
21
|
throw createError({ statusCode: status, data: err?.data ?? null, statusMessage: 'login_failed', message: loginErrorMessage(status, err?.data) })
|
|
22
22
|
}
|
|
23
23
|
const { token, body } = normalizeLoginResponse(data)
|
|
24
|
-
|
|
24
|
+
// Sin token (cambio forzado / OTP / 2FA): no hay sesión que sellar.
|
|
25
|
+
if (token) await writeSession(event, { token, currentContext: context })
|
|
25
26
|
return body
|
|
26
27
|
})
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { defineEventHandler, readBody } from 'h3'
|
|
2
|
+
import { forwardAuthStep } from '../../../utils/authStep'
|
|
3
|
+
|
|
4
|
+
// Verifica el código OTP que el login envió por correo y sella la sesión.
|
|
5
|
+
export default defineEventHandler(async (event) => {
|
|
6
|
+
const { context, user_id, code, action = 'login' } = await readBody(event)
|
|
7
|
+
return forwardAuthStep(event, 'auth/otp/verify', { user_id, code, action, context }, context)
|
|
8
|
+
})
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineEventHandler, readBody } from 'h3'
|
|
2
|
+
import { forwardAuthStep } from '../../../utils/authStep'
|
|
3
|
+
|
|
4
|
+
// Cambio de clave forzado (pre-auth): cambia la clave (con el código OTP si el
|
|
5
|
+
// backend lo exige) y sella la sesión con el token que devuelve Laravel.
|
|
6
|
+
export default defineEventHandler(async (event) => {
|
|
7
|
+
const { context, user_id, code, password, password_confirmation } = await readBody(event)
|
|
8
|
+
return forwardAuthStep(event, 'auth/password/change', { user_id, code, password, password_confirmation, context }, context)
|
|
9
|
+
})
|
|
@@ -23,6 +23,6 @@ export default defineEventHandler(async (event) => {
|
|
|
23
23
|
throw createError({ statusCode: status, data: err?.data ?? null, statusMessage: 'login_failed', message: loginErrorMessage(status, err?.data) })
|
|
24
24
|
}
|
|
25
25
|
const { token, body } = normalizeLoginResponse(data)
|
|
26
|
-
await writeSession(event, { token })
|
|
26
|
+
if (token) await writeSession(event, { token })
|
|
27
27
|
return body
|
|
28
28
|
})
|
|
@@ -10,6 +10,15 @@ describe('normalizeLoginResponse', () => {
|
|
|
10
10
|
expect(r.body).toEqual({ user: { id: '1' }, contexts: ['backoffice'], requires_password_change: false })
|
|
11
11
|
expect((r.body as any).token).toBeUndefined()
|
|
12
12
|
})
|
|
13
|
+
it('cambio forzado: sin token reenvía user_id y el flag', () => {
|
|
14
|
+
const r = normalizeLoginResponse({ requires_password_change: true, user_id: 'u-1' } as any)
|
|
15
|
+
expect(r.token).toBeUndefined()
|
|
16
|
+
expect(r.body).toMatchObject({ requires_password_change: true, user_id: 'u-1' })
|
|
17
|
+
})
|
|
18
|
+
it('OTP/2FA: reenvía el flag y user_id', () => {
|
|
19
|
+
expect(normalizeLoginResponse({ requires_otp: true, user_id: 'u-2' } as any).body).toMatchObject({ requires_otp: true, user_id: 'u-2' })
|
|
20
|
+
expect(normalizeLoginResponse({ requires_2fa: true, user_id: 'u-3' } as any).body).toMatchObject({ requires_2fa: true, user_id: 'u-3' })
|
|
21
|
+
})
|
|
13
22
|
it('acepta access_token como alias', () => {
|
|
14
23
|
expect(normalizeLoginResponse({ access_token: 'jwt2' }).token).toBe('jwt2')
|
|
15
24
|
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { readBody, createError, getRequestHeader, type H3Event } from 'h3'
|
|
2
|
+
import { writeSession } from './session'
|
|
3
|
+
import { forwardClientHeaders } from './bff'
|
|
4
|
+
import { normalizeLoginResponse } from './login'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Paso pre-auth del login (verificar OTP, cambio de clave forzado): reenvía a
|
|
8
|
+
* Laravel server-to-server y, si responde token, sella la sesión igual que
|
|
9
|
+
* /_auth/login. Devuelve el body SIN token (o el siguiente paso pendiente).
|
|
10
|
+
*/
|
|
11
|
+
export async function forwardAuthStep(event: H3Event, laravelPath: string, payload: Record<string, unknown>, context: string) {
|
|
12
|
+
const config = useRuntimeConfig(event)
|
|
13
|
+
let data: any
|
|
14
|
+
try {
|
|
15
|
+
data = await $fetch(`${config.apiInternalUrl}/${laravelPath}`, {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
body: payload,
|
|
18
|
+
headers: { Accept: 'application/json', ...forwardClientHeaders(n => getRequestHeader(event, n)) },
|
|
19
|
+
})
|
|
20
|
+
} catch (err: any) {
|
|
21
|
+
const status = err?.response?.status ?? err?.statusCode ?? 500
|
|
22
|
+
const msg = status === 429
|
|
23
|
+
? 'Demasiados intentos. Espera un minuto e inténtalo de nuevo.'
|
|
24
|
+
: (err?.data?.errors && Object.values(err.data.errors).flat()[0]) || err?.data?.message || 'No se pudo completar el paso.'
|
|
25
|
+
throw createError({ statusCode: status, data: err?.data ?? null, statusMessage: 'auth_step_failed', message: String(msg) })
|
|
26
|
+
}
|
|
27
|
+
const { token, body } = normalizeLoginResponse(data)
|
|
28
|
+
if (token) await writeSession(event, { token, currentContext: context })
|
|
29
|
+
return body
|
|
30
|
+
}
|
|
31
|
+
|
package/server/utils/login.ts
CHANGED
|
@@ -7,17 +7,27 @@ export interface LaravelLoginResponse {
|
|
|
7
7
|
[k: string]: unknown
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
/**
|
|
10
|
+
/** Pasos previos al token que Laravel puede exigir (todos traen user_id, sin token). */
|
|
11
|
+
export const LOGIN_CHALLENGES = ['requires_otp', 'requires_2fa', 'requires_email_verification'] as const
|
|
12
|
+
|
|
13
|
+
/** Separa el token (va a la sesión) del body que vuelve al cliente (sin token).
|
|
14
|
+
* Sin token (cambio forzado / OTP / 2FA / verificación) se reenvía user_id y el
|
|
15
|
+
* flag: el cliente lo necesita para el paso siguiente (antes se perdía). */
|
|
11
16
|
export function normalizeLoginResponse(data: LaravelLoginResponse) {
|
|
12
17
|
const token = data.token ?? data.access_token
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
contexts: data.contexts ?? [],
|
|
18
|
-
requires_password_change: data.requires_password_change ?? false,
|
|
19
|
-
},
|
|
18
|
+
const body: Record<string, unknown> = {
|
|
19
|
+
user: data.user ?? null,
|
|
20
|
+
contexts: data.contexts ?? [],
|
|
21
|
+
requires_password_change: data.requires_password_change ?? false,
|
|
20
22
|
}
|
|
23
|
+
for (const k of LOGIN_CHALLENGES) if (data[k]) body[k] = true
|
|
24
|
+
if (!token && data.user_id != null) body.user_id = data.user_id
|
|
25
|
+
return { token, body }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** true si la respuesta es un paso previo (no hay sesión que sellar). */
|
|
29
|
+
export function isLoginChallenge(body: Record<string, unknown>): boolean {
|
|
30
|
+
return Boolean(body.requires_password_change) || LOGIN_CHALLENGES.some(k => body[k])
|
|
21
31
|
}
|
|
22
32
|
|
|
23
33
|
/**
|