@innertia-solutions/innertia-nuxt 0.1.72 → 0.1.74
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 +22 -1
- package/package.json +1 -1
- package/plugins/api-organization.client.ts +25 -17
- package/plugins/vue-query.ts +23 -20
- package/server/routes/_auth/otp/verify.post.ts +8 -0
- package/server/routes/_auth/password/change.post.ts +9 -0
- package/server/utils/authStep.ts +31 -0
package/composables/useAuth.js
CHANGED
|
@@ -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
|
@@ -11,27 +11,35 @@
|
|
|
11
11
|
* el header por-request sin afectar el toggle global.
|
|
12
12
|
*
|
|
13
13
|
* Solo activo cuando `mode === 'saas'` (orgs viven dentro de un tenant).
|
|
14
|
+
*
|
|
15
|
+
* Depende de `innertia:vue-query`: useOrganization → useAuth → useQueryClient()
|
|
16
|
+
* necesita el QueryClient instalado. Sin `dependsOn`, el orden alfabético de la
|
|
17
|
+
* capa corre este plugin antes y el cliente cae con "No 'queryClient' found".
|
|
14
18
|
*/
|
|
15
|
-
export default defineNuxtPlugin(
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
export default defineNuxtPlugin({
|
|
20
|
+
name: 'innertia:api-organization',
|
|
21
|
+
dependsOn: ['innertia:vue-query'],
|
|
22
|
+
setup() {
|
|
23
|
+
const { hasTenant } = useInnertiaMode()
|
|
24
|
+
if (!hasTenant()) return
|
|
18
25
|
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
const appConfig = useAppConfig()
|
|
27
|
+
if (!appConfig.innertia?.organizations?.enabled) return
|
|
21
28
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
const { add } = useRequestInterceptors()
|
|
30
|
+
const organizationStore = useOrganizationStore()
|
|
31
|
+
const { currentContext } = useOrganization()
|
|
25
32
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
add((headers: Record<string, string>) => {
|
|
34
|
+
const ctx = currentContext.value
|
|
35
|
+
if (!ctx) return
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
37
|
+
const slug = organizationStore.currentFor(ctx)
|
|
38
|
+
if (slug) headers['X-Organization'] = slug
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
if (organizationStore.consolidated) {
|
|
41
|
+
headers['X-Consolidated'] = 'true'
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
},
|
|
37
45
|
})
|
package/plugins/vue-query.ts
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
import type { DehydratedState } from '@tanstack/vue-query'
|
|
2
2
|
import { VueQueryPlugin, QueryClient, dehydrate, hydrate } from '@tanstack/vue-query'
|
|
3
3
|
|
|
4
|
-
export default defineNuxtPlugin(
|
|
5
|
-
|
|
4
|
+
export default defineNuxtPlugin({
|
|
5
|
+
name: 'innertia:vue-query',
|
|
6
|
+
setup(nuxtApp) {
|
|
7
|
+
const vueQueryState = useState<DehydratedState | null>('vue-query')
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
const queryClient = new QueryClient({
|
|
10
|
+
defaultOptions: {
|
|
11
|
+
queries: {
|
|
12
|
+
staleTime: 5 * 60 * 1000, // 5 min — datos frescos sin re-fetch
|
|
13
|
+
gcTime: 10 * 60 * 1000, // 10 min — cache se limpia si no hay subscribers
|
|
14
|
+
retry: 1,
|
|
15
|
+
refetchOnWindowFocus: false,
|
|
16
|
+
},
|
|
14
17
|
},
|
|
15
|
-
}
|
|
16
|
-
})
|
|
18
|
+
})
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
nuxtApp.vueApp.use(VueQueryPlugin, { queryClient })
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
if (import.meta.server) {
|
|
23
|
+
nuxtApp.hooks.hook('app:rendered', () => {
|
|
24
|
+
vueQueryState.value = dehydrate(queryClient)
|
|
25
|
+
})
|
|
26
|
+
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
if (import.meta.client) {
|
|
29
|
+
hydrate(queryClient, vueQueryState.value)
|
|
30
|
+
}
|
|
31
|
+
},
|
|
29
32
|
})
|
|
@@ -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
|
+
|