nuxt-authenticate-module 1.0.0

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.
Files changed (84) hide show
  1. package/README.md +332 -0
  2. package/dist/module.d.mts +10 -0
  3. package/dist/module.json +9 -0
  4. package/dist/module.mjs +79 -0
  5. package/dist/runtime/assets/main.css +1 -0
  6. package/dist/runtime/components/UserInfo.d.vue.ts +2 -0
  7. package/dist/runtime/components/UserInfo.vue +29 -0
  8. package/dist/runtime/components/UserInfo.vue.d.ts +2 -0
  9. package/dist/runtime/composable/closeSession.d.ts +3 -0
  10. package/dist/runtime/composable/closeSession.js +13 -0
  11. package/dist/runtime/middleware/protected.d.ts +2 -0
  12. package/dist/runtime/middleware/protected.js +6 -0
  13. package/dist/runtime/page/UserLogin.d.vue.ts +2 -0
  14. package/dist/runtime/page/UserLogin.vue +112 -0
  15. package/dist/runtime/page/UserLogin.vue.d.ts +2 -0
  16. package/dist/runtime/page/UserRegister.d.vue.ts +2 -0
  17. package/dist/runtime/page/UserRegister.vue +150 -0
  18. package/dist/runtime/page/UserRegister.vue.d.ts +2 -0
  19. package/dist/runtime/server/api/auth/closeSession.post.d.ts +4 -0
  20. package/dist/runtime/server/api/auth/closeSession.post.js +7 -0
  21. package/dist/runtime/server/api/auth/login.post.d.ts +5 -0
  22. package/dist/runtime/server/api/auth/login.post.js +9 -0
  23. package/dist/runtime/server/api/auth/register.post.d.ts +5 -0
  24. package/dist/runtime/server/api/auth/register.post.js +8 -0
  25. package/dist/runtime/server/middleware/session.d.ts +2 -0
  26. package/dist/runtime/server/middleware/session.js +16 -0
  27. package/dist/runtime/server/modules/auth/auth.errors.d.ts +4 -0
  28. package/dist/runtime/server/modules/auth/auth.errors.js +32 -0
  29. package/dist/runtime/server/modules/auth/auth.user.d.ts +24 -0
  30. package/dist/runtime/server/modules/auth/auth.user.js +10 -0
  31. package/dist/runtime/server/modules/auth/index.d.ts +2 -0
  32. package/dist/runtime/server/modules/auth/index.js +2 -0
  33. package/dist/runtime/server/modules/functions/errors/errors.controler.d.ts +6 -0
  34. package/dist/runtime/server/modules/functions/errors/errors.controler.js +15 -0
  35. package/dist/runtime/server/modules/functions/errors/errors.model.d.ts +10 -0
  36. package/dist/runtime/server/modules/functions/errors/index.d.ts +2 -0
  37. package/dist/runtime/server/modules/functions/errors/index.js +1 -0
  38. package/dist/runtime/server/modules/functions/utils/comparePassword.d.ts +1 -0
  39. package/dist/runtime/server/modules/functions/utils/comparePassword.js +4 -0
  40. package/dist/runtime/server/modules/functions/utils/hashPassword.d.ts +1 -0
  41. package/dist/runtime/server/modules/functions/utils/hashPassword.js +4 -0
  42. package/dist/runtime/server/modules/session/index.d.ts +4 -0
  43. package/dist/runtime/server/modules/session/index.js +3 -0
  44. package/dist/runtime/server/modules/session/session.close.d.ts +12 -0
  45. package/dist/runtime/server/modules/session/session.close.js +7 -0
  46. package/dist/runtime/server/modules/session/session.errors.d.ts +4 -0
  47. package/dist/runtime/server/modules/session/session.errors.js +16 -0
  48. package/dist/runtime/server/modules/session/session.register.d.ts +21 -0
  49. package/dist/runtime/server/modules/session/session.register.js +5 -0
  50. package/dist/runtime/server/modules/session/session.types.d.ts +31 -0
  51. package/dist/runtime/server/modules/users/index.d.ts +5 -0
  52. package/dist/runtime/server/modules/users/index.js +4 -0
  53. package/dist/runtime/server/modules/users/user.checkIsExist.d.ts +14 -0
  54. package/dist/runtime/server/modules/users/user.checkIsExist.js +8 -0
  55. package/dist/runtime/server/modules/users/user.errors.d.ts +4 -0
  56. package/dist/runtime/server/modules/users/user.errors.js +64 -0
  57. package/dist/runtime/server/modules/users/user.register.d.ts +21 -0
  58. package/dist/runtime/server/modules/users/user.register.js +10 -0
  59. package/dist/runtime/server/modules/users/user.repository.d.ts +10 -0
  60. package/dist/runtime/server/modules/users/user.validator.d.ts +9 -0
  61. package/dist/runtime/server/modules/users/user.validator.js +16 -0
  62. package/dist/runtime/server/service/index.d.ts +3 -0
  63. package/dist/runtime/server/service/index.js +3 -0
  64. package/dist/runtime/server/service/login.service.d.ts +8 -0
  65. package/dist/runtime/server/service/login.service.js +13 -0
  66. package/dist/runtime/server/service/registrarionUser.service.d.ts +5 -0
  67. package/dist/runtime/server/service/registrarionUser.service.js +6 -0
  68. package/dist/runtime/server/service/sesionClose.service.d.ts +5 -0
  69. package/dist/runtime/server/service/sesionClose.service.js +8 -0
  70. package/dist/runtime/server/tsconfig.json +3 -0
  71. package/dist/runtime/server/types/auth.d.ts +25 -0
  72. package/dist/runtime/server/utility/getSessionRepository.d.ts +13 -0
  73. package/dist/runtime/server/utility/getSessionRepository.js +10 -0
  74. package/dist/runtime/server/utility/getUserRepository.d.ts +13 -0
  75. package/dist/runtime/server/utility/getUserRepository.js +10 -0
  76. package/dist/runtime/server/utility/index.d.ts +2 -0
  77. package/dist/runtime/server/utility/index.js +2 -0
  78. package/dist/runtime/tsconfig.json +3 -0
  79. package/dist/runtime/utility/fetchError.d.ts +28 -0
  80. package/dist/runtime/utility/fetchError.js +4 -0
  81. package/dist/runtime/utility/index.d.ts +1 -0
  82. package/dist/runtime/utility/index.js +1 -0
  83. package/dist/types.d.mts +3 -0
  84. package/package.json +69 -0
@@ -0,0 +1,9 @@
1
+ import type { UserModel } from '@/generated/prisma/models';
2
+ export interface payLoadUserValidator {
3
+ userName: UserModel['userName'];
4
+ password: UserModel['password'];
5
+ name: UserModel['name'];
6
+ lastName: UserModel['lastName'];
7
+ personId: UserModel['personId'];
8
+ }
9
+ export declare function userValidate(user: payLoadUserValidator): void;
@@ -0,0 +1,16 @@
1
+ import { userErrors } from "./user.errors.js";
2
+ const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
3
+ const personIdRegex = /^\d{1,4}-\d{1,4}-\d{1,4}$/;
4
+ export function userValidate(user) {
5
+ if (!user.userName) throw userErrors.error("EMPTY");
6
+ if (!user.password) throw userErrors.error("EMPTY");
7
+ if (!user.name) throw userErrors.error("EMPTY");
8
+ if (!user.lastName) throw userErrors.error("EMPTY");
9
+ if (!user.personId) throw userErrors.error("EMPTY");
10
+ if (!passwordRegex.test(user.password)) {
11
+ throw userErrors.error("PASSWORD_NOT_VALID");
12
+ }
13
+ if (!personIdRegex.test(user.personId)) {
14
+ throw userErrors.error("PERSONID_NOT_VALID");
15
+ }
16
+ }
@@ -0,0 +1,3 @@
1
+ export * from './registrarionUser.service.js';
2
+ export * from './login.service.js';
3
+ export * from './sesionClose.service.js';
@@ -0,0 +1,3 @@
1
+ export * from "./registrarionUser.service.js";
2
+ export * from "./login.service.js";
3
+ export * from "./sesionClose.service.js";
@@ -0,0 +1,8 @@
1
+ import type { LoginData } from 'modules/auth';
2
+ import type { ISessionRepository } from 'modules/session';
3
+ import type { IUserRepository } from 'modules/users';
4
+ import type { H3Event } from 'h3';
5
+ export declare function handleUserLogin(event: H3Event, userRepository: IUserRepository, user: LoginData, sessionRepository: ISessionRepository): Promise<{
6
+ id: number;
7
+ user: string;
8
+ }>;
@@ -0,0 +1,13 @@
1
+ import { authUser } from "modules/auth";
2
+ export async function handleUserLogin(event, userRepository, user, sessionRepository) {
3
+ const authenticatedUser = await authUser(userRepository, user);
4
+ const { id: sessionId } = await sessionRepository.createSession(authenticatedUser.id);
5
+ await setUserSession(event, {
6
+ user: {
7
+ name: user.userName,
8
+ sessionId,
9
+ activityTime: (/* @__PURE__ */ new Date()).getTime()
10
+ }
11
+ });
12
+ return { id: authenticatedUser.id, user: authenticatedUser.userName };
13
+ }
@@ -0,0 +1,5 @@
1
+ import type { IUserRepository, PayloadUserRegister } from 'modules/users';
2
+ export declare function handleUserRegistration(userRepository: IUserRepository, userData: PayloadUserRegister): Promise<{
3
+ id: number;
4
+ userName: string;
5
+ }>;
@@ -0,0 +1,6 @@
1
+ import { userValidate, register } from "modules/users";
2
+ export async function handleUserRegistration(userRepository, userData) {
3
+ userValidate(userData);
4
+ const { userName, id } = await register(userRepository, userData);
5
+ return { id, userName };
6
+ }
@@ -0,0 +1,5 @@
1
+ import { type ISessionRepository } from 'modules/session';
2
+ import type { H3Event } from 'h3';
3
+ export declare function handleCloseSession(event: H3Event, sessionRepository: ISessionRepository): Promise<{
4
+ message: string;
5
+ }>;
@@ -0,0 +1,8 @@
1
+ import { sessionClose } from "modules/session";
2
+ export async function handleCloseSession(event, sessionRepository) {
3
+ const session = await getUserSession(event);
4
+ if (session.user)
5
+ await sessionClose(sessionRepository, session.user?.sessionId);
6
+ await clearUserSession(event);
7
+ return { message: "Sesi\xF3n cerrada con \xE9xito" };
8
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../playground/.nuxt/tsconfig.server.json",
3
+ }
@@ -0,0 +1,25 @@
1
+ import type { IUserRepository } from '../modules/users/user.repository'
2
+ import type { ISessionRepository } from '../modules/session/session.types'
3
+
4
+ declare module '#auth-utils' {
5
+ interface User {
6
+ name: string
7
+ sessionId: number
8
+ activityTime: number
9
+ }
10
+
11
+ interface UserSession {
12
+ user: User
13
+ }
14
+ }
15
+
16
+ // Extender el contexto de H3 para agregar sessionId
17
+ declare module 'h3' {
18
+ interface H3EventContext {
19
+ sessionId?: number
20
+ userRepository?: IUserRepository
21
+ sessionRepository?: ISessionRepository
22
+ }
23
+ }
24
+
25
+ export {}
@@ -0,0 +1,13 @@
1
+ import type { H3Event } from 'h3';
2
+ import type { ISessionRepository } from 'modules/session';
3
+ /**
4
+ * Obtiene el repositorio de sesiones desde el contexto del evento H3.
5
+ *
6
+ * Extrae `sessionRepository` de `event.context` y lo devuelve tipado como `ISessionRepository`.
7
+ * Si no está presente, lanza un error (status 500) indicando que debe inyectarse mediante middleware.
8
+ *
9
+ * @param event - Evento H3 que contiene el contexto con el repositorio de sesiones.
10
+ * @returns El repositorio de sesiones (`ISessionRepository`).
11
+ * @throws {Error} Si `sessionRepository` no está configurado en `event.context`.
12
+ */
13
+ export declare function getSessionRepository(event: H3Event): ISessionRepository;
@@ -0,0 +1,10 @@
1
+ export function getSessionRepository(event) {
2
+ const sessionRepository = event.context.sessionRepository;
3
+ if (!sessionRepository) {
4
+ throw createError({
5
+ statusCode: 500,
6
+ message: "Session repository not configured. Please inject it via middleware."
7
+ });
8
+ }
9
+ return sessionRepository;
10
+ }
@@ -0,0 +1,13 @@
1
+ import type { H3Event } from 'h3';
2
+ import type { IUserRepository } from '../modules/users/index.js';
3
+ /**
4
+ * Obtiene el repositorio de usuarios desde el contexto del evento H3.
5
+ *
6
+ * Extrae `userRepository` de `event.context` y lo devuelve tipado como `IUserRepository`.
7
+ * Si no está presente, lanza un error (status 500) indicando que debe inyectarse mediante middleware.
8
+ *
9
+ * @param event - Evento H3 que contiene el contexto con el repositorio de usuarios.
10
+ * @returns El repositorio de usuarios (`IUserRepository`).
11
+ * @throws {Error} Si `userRepository` no está configurado en `event.context`.
12
+ */
13
+ export declare function getUserRepository(event: H3Event): IUserRepository;
@@ -0,0 +1,10 @@
1
+ export function getUserRepository(event) {
2
+ const userRepository = event.context.userRepository;
3
+ if (!userRepository) {
4
+ throw createError({
5
+ statusCode: 500,
6
+ message: "User repository not configured. Please inject it via middleware."
7
+ });
8
+ }
9
+ return userRepository;
10
+ }
@@ -0,0 +1,2 @@
1
+ export * from './getUserRepository.js';
2
+ export * from './getSessionRepository.js';
@@ -0,0 +1,2 @@
1
+ export * from "./getUserRepository.js";
2
+ export * from "./getSessionRepository.js";
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../playground/.nuxt/tsconfig.json"
3
+ }
@@ -0,0 +1,28 @@
1
+ export interface FetchError {
2
+ statusCode: number;
3
+ data: {
4
+ message: string;
5
+ };
6
+ }
7
+ /**
8
+ * Composable que devuelve una referencia reactiva para gestionar errores de solicitudes de red.
9
+ *
10
+ * Esta función expone una `ref` cuyo valor inicial es `null` y que puede
11
+ * actualizarse con una instancia de `FetchError` cuando una operación de
12
+ * obtención de datos falle. Es útil para centralizar y observar el estado de
13
+ * errores en componentes o en otros composables.
14
+ *
15
+ * @returns Ref<FetchError | null> Referencia reactiva con el último error de fetch o `null` si no hay error.
16
+ *
17
+ * @example
18
+ * const fetchError = useFetchError()
19
+ * try {
20
+ * const data = await $fetch('/api/items')
21
+ * } catch (err) {
22
+ * fetchError.value = err as FetchError
23
+ * }
24
+ * if (fetchError.value) {
25
+ * console.error(fetchError.value.message)
26
+ * }
27
+ */
28
+ export declare function useFetchError(): any;
@@ -0,0 +1,4 @@
1
+ export function useFetchError() {
2
+ const errorRef = ref(null);
3
+ return errorRef;
4
+ }
@@ -0,0 +1 @@
1
+ export * from './fetchError.js';
@@ -0,0 +1 @@
1
+ export * from "./fetchError.js";
@@ -0,0 +1,3 @@
1
+ export { default } from './module.mjs'
2
+
3
+ export { type ModuleOptions } from './module.mjs'
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "nuxt-authenticate-module",
3
+ "version": "1.0.0",
4
+ "description": "Módulo de autenticación con inversión de dependencias",
5
+ "repository": "jmg03ndorg/authenticate-module",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/types.d.mts",
11
+ "import": "./dist/module.mjs"
12
+ }
13
+ },
14
+ "main": "./dist/module.mjs",
15
+ "typesVersions": {
16
+ "*": {
17
+ ".": [
18
+ "./dist/types.d.mts"
19
+ ]
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "dependencies": {
26
+ "@nuxt/kit": "4.2.2",
27
+ "@nuxt/ui": "https://pkg.pr.new/@nuxt/ui@5106",
28
+ "@prisma/adapter-mssql": "^7.2.0",
29
+ "@prisma/client": "^7.2.0",
30
+ "bcrypt": "6.0.0",
31
+ "mssql": "^12.2.0"
32
+ },
33
+ "devDependencies": {
34
+ "@iconify-json/lucide": "^1.2.82",
35
+ "@iconify-json/majesticons": "^1.2.4",
36
+ "@iconify-json/material-symbols": "^1.2.50",
37
+ "@nuxt/devtools": "^2.7.0",
38
+ "@nuxt/eslint-config": "1.9.0",
39
+ "@nuxt/module-builder": "1.0.2",
40
+ "@nuxt/schema": "4.2.2",
41
+ "@nuxt/test-utils": "^3.21.0",
42
+ "@types/bcrypt": "6.0.0",
43
+ "@types/jsonwebtoken": "9.0.10",
44
+ "@types/mssql": "^9.1.8",
45
+ "@types/node": "latest",
46
+ "baseline-browser-mapping": "^2.9.11",
47
+ "changelogen": "0.6.2",
48
+ "dotenv": "^17.2.3",
49
+ "eslint": "^9.39.1",
50
+ "happy-dom": "^20.0.11",
51
+ "magicast": "^0.3.5",
52
+ "nuxt": "4.2.2",
53
+ "nuxt-auth-utils": "^0.5.26",
54
+ "prisma": "^7.2.0",
55
+ "typescript": "5.9.2",
56
+ "vitest": "3.2.4",
57
+ "vue-tsc": "3.0.6"
58
+ },
59
+ "scripts": {
60
+ "dev": "npm run dev:prepare && nuxi dev playground",
61
+ "dev:build": "nuxi build playground",
62
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
63
+ "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
64
+ "lint": "eslint .",
65
+ "test": "vitest run",
66
+ "test:watch": "vitest watch",
67
+ "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
68
+ }
69
+ }