diff --git a/src/i18n/en/register.json b/src/i18n/en/register.json index b05b88b..e8b610d 100644 --- a/src/i18n/en/register.json +++ b/src/i18n/en/register.json @@ -8,10 +8,11 @@ "password": "Password", "password_error": "This password is not valid.", "password_validation_1": "Minimum 8 characters", - "password_validation_2": "One uppercase", - "password_validation_3": "One lowercase", - "password_validation_4": "One number", - "password_validation_5": "One special character", + "password_validation_2": "Maximum 32 characters", + "password_validation_3": "One uppercase", + "password_validation_4": "One lowercase", + "password_validation_5": "One number", + "password_validation_6": "One special character", "repeat_password": "Repeat password", "repeat_password_error": "Passwords do not match.", "question": "Already have an account?", diff --git a/src/i18n/es/register.json b/src/i18n/es/register.json index 1868267..67b517c 100644 --- a/src/i18n/es/register.json +++ b/src/i18n/es/register.json @@ -8,10 +8,11 @@ "password": "Contraseña", "password_error": "Esta contraseña no es válida.", "password_validation_1": "Mínimo 8 caracteres", - "password_validation_2": "Una mayúscula", - "password_validation_3": "Una minúscula", - "password_validation_4": "Un número", - "password_validation_5": "Un carácter especial", + "password_validation_2": "Máximo 32 caracteres", + "password_validation_3": "Una mayúscula", + "password_validation_4": "Una minúscula", + "password_validation_5": "Un número", + "password_validation_6": "Un carácter especial", "repeat_password": "Repetir contraseña", "repeat_password_error": "Las contraseñas no coinciden.", "question": "¿Ya tienes una cuenta?", diff --git a/src/middleware/auth.ts b/src/middleware/01.refreshSession.global.ts similarity index 74% rename from src/middleware/auth.ts rename to src/middleware/01.refreshSession.global.ts index abd9e63..311a5a4 100644 --- a/src/middleware/auth.ts +++ b/src/middleware/01.refreshSession.global.ts @@ -1,6 +1,4 @@ export default defineNuxtRouteMiddleware(async (middleware) => { - const nuxtApp = useNuxtApp(); - const localeRoute = useLocaleRoute(); const { loggedIn, fetch, session, clear } = useUserSession(); const { extractTokenData } = useJwt(); @@ -25,12 +23,4 @@ export default defineNuxtRouteMiddleware(async (middleware) => { await fetch(); } } - - // Si el usuario no está logueado, lo redirige al login - if (!loggedIn.value) { - const loginRoute = localeRoute('login', nuxtApp.$i18n.locale); - const loginPath = loginRoute != null ? loginRoute.path : '/'; - - return navigateTo(loginPath); - } }); diff --git a/src/middleware/disabledWithSession.ts b/src/middleware/disabledWithSession.ts new file mode 100644 index 0000000..7de2f28 --- /dev/null +++ b/src/middleware/disabledWithSession.ts @@ -0,0 +1,13 @@ +export default defineNuxtRouteMiddleware(async (middleware) => { + const nuxtApp = useNuxtApp(); + const localeRoute = useLocaleRoute(); + const { loggedIn } = useUserSession(); + + // Si el usuario está logueado, lo redirige a su biblioteca + if (loggedIn.value) { + const libraryRoute = localeRoute('library', nuxtApp.$i18n.locale.value); + const libraryPath = libraryRoute != null ? libraryRoute.path : '/'; + + return navigateTo(libraryPath); + } +}); diff --git a/src/middleware/enabledWithSession.ts b/src/middleware/enabledWithSession.ts new file mode 100644 index 0000000..2ca796f --- /dev/null +++ b/src/middleware/enabledWithSession.ts @@ -0,0 +1,13 @@ +export default defineNuxtRouteMiddleware(async (middleware) => { + const nuxtApp = useNuxtApp(); + const localeRoute = useLocaleRoute(); + const { loggedIn } = useUserSession(); + + // Si el usuario no está logueado, lo redirige al login + if (!loggedIn.value) { + const loginRoute = localeRoute('login', nuxtApp.$i18n.locale.value); + const loginPath = loginRoute != null ? loginRoute.path : '/'; + + return navigateTo(loginPath); + } +}); diff --git a/src/pages/(user)/library.vue b/src/pages/(user)/library.vue index 1eaef24..f9ec36a 100644 --- a/src/pages/(user)/library.vue +++ b/src/pages/(user)/library.vue @@ -10,7 +10,7 @@ defineI18nRoute({ }); definePageMeta({ - middleware: ["auth"], + middleware: ["enabled-with-session"], }); useHead({ diff --git a/src/pages/auth/login.vue b/src/pages/auth/login.vue index d21aa9d..9ac7331 100644 --- a/src/pages/auth/login.vue +++ b/src/pages/auth/login.vue @@ -11,6 +11,10 @@ defineI18nRoute({ }, }); +definePageMeta({ + middleware: ["disabled-with-session"], +}); + useHead({ title: t('home.title') + ' - ' + t('login.title'), meta: [ diff --git a/src/pages/auth/register.vue b/src/pages/auth/register.vue index 35df7c9..d605585 100644 --- a/src/pages/auth/register.vue +++ b/src/pages/auth/register.vue @@ -1,5 +1,10 @@ @@ -27,7 +81,7 @@ useHead({ - + @@ -35,36 +89,44 @@ useHead({ {{ t('register.email') }} - {{ t('register.email_error') }} + {{ t('register.email_error') }} {{ t('register.password') }} - {{ t('register.password_error') }} - {{ t('register.password_validation_1') }} - {{ t('register.password_validation_2') }} - {{ t('register.password_validation_3') }} - {{ t('register.password_validation_4') }} - {{ t('register.password_validation_5') }} + {{ t('register.password_error') }} + + {{ t('register.password_validation_1') }} + {{ t('register.password_validation_2') }} + {{ t('register.password_validation_3') }} + {{ t('register.password_validation_4') }} + {{ t('register.password_validation_5') }} + {{ t('register.password_validation_6') }} {{ t('register.repeat_password') }} - {{ t('register.repeat_password_error') }} + {{ t('register.repeat_password_error') }} diff --git a/src/pages/auth/reset.vue b/src/pages/auth/reset.vue index f7282c3..5048596 100644 --- a/src/pages/auth/reset.vue +++ b/src/pages/auth/reset.vue @@ -8,6 +8,10 @@ defineI18nRoute({ }, }); +definePageMeta({ + middleware: ["disabled-with-session"], +}); + useHead({ title: t('home.title') + ' - ' + t('reset.title'), meta: [ diff --git a/src/server/api/auth/login.post.ts b/src/server/api/auth/login.post.ts index a119936..2d565e3 100644 --- a/src/server/api/auth/login.post.ts +++ b/src/server/api/auth/login.post.ts @@ -1,10 +1,10 @@ -import { LoginAndRefreshResponse } from "~/types/auth"; +import { SessionResponse } from "~/types/auth"; export default defineEventHandler(async (event) => { const config = useRuntimeConfig(); const body = await readBody(event); - const response = await $fetch(config.public.backendUrl + '/auth/login', { + const response = await $fetch(config.public.backendUrl + '/auth/login', { method: 'POST', body: JSON.stringify({ email: body.email, diff --git a/src/server/api/auth/register.post.ts b/src/server/api/auth/register.post.ts new file mode 100644 index 0000000..4d6206d --- /dev/null +++ b/src/server/api/auth/register.post.ts @@ -0,0 +1,31 @@ +import { SessionResponse } from "~/types/auth"; + +export default defineEventHandler(async (event) => { + const config = useRuntimeConfig(); + const body = await readBody(event); + + const response = await $fetch(config.public.backendUrl + '/auth/register', { + method: 'POST', + body: JSON.stringify({ + email: body.email, + password: body.password, + }), + }); + + await replaceUserSession(event, { + user: { + email: response.user.email, + avatar: response.user.avatar, + isAdmin: response.user.isAdmin, + isVerified: response.user.isVerified, + }, + access_token: response.access_token, + refresh_token: response.refresh_token, + }, { + maxAge: Number(config.nuxtSessionTime), + }); + + return { + success: true, + }; +}); diff --git a/src/types/auth.d.ts b/src/types/auth.d.ts index df43625..f8cf178 100644 --- a/src/types/auth.d.ts +++ b/src/types/auth.d.ts @@ -17,4 +17,21 @@ declare module '#auth-utils' { interface UserSession extends LoginData {} } -export interface LoginAndRefreshResponse extends LoginData {} +export interface SessionResponse extends LoginData {} + +export interface RegisterErrorResponse { + data: { + message: string[]; + error: string; + statusCode: number; + }; + message: string; + stack: string; + statusCode: number; + statusMessage: string; + url: string; +} + +export interface SessionSuccessResponse { + success: boolean; +}