@innertia-solutions/innertia-nuxt 0.1.72 → 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.
@@ -24,6 +24,27 @@ export function useAuth() {
24
24
  if (!data?.user && (data?.requires_password_change || data?.requires_otp || data?.requires_2fa || data?.requires_email_verification)) {
25
25
  return data
26
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) {
27
48
  authStore.setAuthenticated(true)
28
49
  if (data.user) authStore.saveUser(data.user)
29
50
  authStore.setCurrentContext(context)
@@ -99,5 +120,5 @@ export function useAuth() {
99
120
  return data.url ?? data
100
121
  }
101
122
 
102
- return { performLogin, fetchMe, logout, getOauthRedirectUrl }
123
+ return { performLogin, verifyOtp, changeForcedPassword, fetchMe, logout, getOauthRedirectUrl }
103
124
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innertia-solutions/innertia-nuxt",
3
- "version": "0.1.72",
3
+ "version": "0.1.73",
4
4
  "description": "Innertia Solutions — Nuxt layer unificada: core, auth, multitenancy, theme y app contexts",
5
5
  "keywords": [
6
6
  "nuxt",
@@ -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
+ })
@@ -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
+