@innertia-solutions/innertia-nuxt 0.1.71 → 0.1.72
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
CHANGED
|
@@ -19,6 +19,11 @@ 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
|
+
}
|
|
22
27
|
authStore.setAuthenticated(true)
|
|
23
28
|
if (data.user) authStore.saveUser(data.user)
|
|
24
29
|
authStore.setCurrentContext(context)
|
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
|
})
|
|
@@ -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
|
})
|
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
|
/**
|