@ministerjs/auth 2.0.0 → 2.0.2

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/package.json CHANGED
@@ -1,24 +1,29 @@
1
1
  {
2
2
  "name": "@ministerjs/auth",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "license": "UNLICENSED",
5
5
  "private": false,
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
9
  "files": [
10
- "dist/*"
10
+ "dist/*",
11
+ "types/*"
11
12
  ],
13
+ "types": "./types",
12
14
  "exports": {
15
+ "./types": {
16
+ "types": "./types/index.d.ts"
17
+ },
13
18
  "./vue": {
14
- "types": "./dist/index.d.ts",
15
- "import": "./dist/index.mjs",
16
- "require": "./dist/index.js"
19
+ "types": "./dist/vue.d.ts",
20
+ "import": "./dist/vue.mjs",
21
+ "require": "./dist/vue.js"
17
22
  },
18
23
  "./nestjs": {
19
- "types": "./dist/index.d.ts",
20
- "import": "./dist/index.mjs",
21
- "require": "./dist/index.js"
24
+ "types": "./dist/nestjs.d.ts",
25
+ "import": "./dist/nestjs.mjs",
26
+ "require": "./dist/nestjs.js"
22
27
  }
23
28
  },
24
29
  "peerDependencies": {
@@ -60,6 +65,7 @@
60
65
  "dev": "tsup --watch",
61
66
  "lint": "eslint ./src --fix --config ../../eslint.config.mjs",
62
67
  "test": "vitest run --root ../../ --project @ministerjs/auth",
63
- "build": "pnpm lint && pnpm test && tsup"
68
+ "build:types": "rimraf types && tsc --project tsconfig.build.json && cpx \"src/**/*.d.ts\" types/",
69
+ "build": "pnpm lint && pnpm test && pnpm build:types && tsup"
64
70
  }
65
71
  }
@@ -0,0 +1,2 @@
1
+ export * from "./vue";
2
+ export * from "./nestjs";
@@ -0,0 +1,10 @@
1
+ import type { Request, Response } from "express";
2
+ import type { AuthDriver, AuthPayload, AuthResponse, LogoutResponse } from "./types";
3
+ declare class AuthController<User extends Record<string, any> = Record<string, any>> {
4
+ private readonly driver;
5
+ constructor(driver: AuthDriver<User>);
6
+ login(body: AuthPayload, request: Request, response: Response): Promise<AuthResponse<User>>;
7
+ checkIn(request: Request, response: Response): Promise<AuthResponse<User>>;
8
+ logout(request: Request, response: Response): Promise<LogoutResponse>;
9
+ }
10
+ export { AuthController };
@@ -0,0 +1,32 @@
1
+ import { DynamicModule, Type } from "@nestjs/common";
2
+ import type { AuthDriver } from "./types";
3
+ type DriverProvider<User> = AuthDriver<User> | {
4
+ useValue: AuthDriver<User>;
5
+ } | {
6
+ useClass: Type<AuthDriver<User>>;
7
+ } | {
8
+ useFactory: (...args: any[]) => Promise<AuthDriver<User>> | AuthDriver<User>;
9
+ inject?: any[];
10
+ } | Type<AuthDriver<User>>;
11
+ interface AuthModuleOptions<User = any> {
12
+ /**
13
+ * Provider que implementa a interface AuthDriver.
14
+ * Aceita instância direta, classe Nest provider ou useFactory.
15
+ */
16
+ driver: DriverProvider<User>;
17
+ }
18
+ interface AuthModuleAsyncOptions<User = any> {
19
+ imports?: any[];
20
+ /**
21
+ * Factory que devolve uma instância de AuthDriver.
22
+ */
23
+ useFactory: (...args: any[]) => Promise<AuthDriver<User>> | AuthDriver<User>;
24
+ inject?: any[];
25
+ }
26
+ declare class AuthModule {
27
+ static register<User = any>(options: AuthModuleOptions<User>): DynamicModule;
28
+ static registerAsync<User = any>(options: AuthModuleAsyncOptions<User>): DynamicModule;
29
+ private static normalizeDriverProvider;
30
+ }
31
+ export type { AuthModuleOptions, AuthModuleAsyncOptions, DriverProvider };
32
+ export { AuthModule };
@@ -0,0 +1,65 @@
1
+ import type { CookieOptions, Request, Response } from "express";
2
+ import { type SignOptions } from "jsonwebtoken";
3
+ import type { AuthDriver, AuthPayload } from "./types";
4
+ type PrismaClientLike = Record<string, any>;
5
+ interface JwtPrismaDriverOptions {
6
+ prisma: PrismaClientLike;
7
+ /**
8
+ * Nome do model Prisma (ex.: "user").
9
+ */
10
+ modelName: string;
11
+ /**
12
+ * Campo usado como identificador de login (ex.: "email").
13
+ */
14
+ identifierField?: string;
15
+ /**
16
+ * Campo usado como senha (hash) no banco.
17
+ */
18
+ passwordField?: string;
19
+ /**
20
+ * Campo de ID primário (usado no payload do token).
21
+ */
22
+ idField?: string;
23
+ /**
24
+ * Segredo para assinar/verificar JWT.
25
+ */
26
+ jwtSecret: string;
27
+ /**
28
+ * Tempo de expiração (aceita formato jsonwebtoken, ex.: "7d").
29
+ */
30
+ jwtExpiresIn?: SignOptions["expiresIn"];
31
+ /**
32
+ * Nome do cookie que armazena o token.
33
+ */
34
+ cookieName?: string;
35
+ /**
36
+ * Opções do cookie (secure, sameSite etc).
37
+ */
38
+ cookieOptions?: Partial<CookieOptions>;
39
+ /**
40
+ * Função de comparação de senha (p.ex. bcrypt.compare).
41
+ */
42
+ comparePassword?: (provided: string, stored: unknown) => Promise<boolean> | boolean;
43
+ /**
44
+ * Permite alterar a shape do usuário retornado.
45
+ */
46
+ transformUser?: <T extends Record<string, any>>(user: T) => any;
47
+ }
48
+ declare class JwtPrismaCookieAuthDriver<User extends Record<string, any>> implements AuthDriver<User> {
49
+ private readonly prismaModel;
50
+ private readonly identifierField;
51
+ private readonly passwordField;
52
+ private readonly idField;
53
+ private readonly jwtSecret;
54
+ private readonly jwtExpiresIn;
55
+ private readonly cookieName;
56
+ private readonly cookieOptions;
57
+ private readonly comparePassword;
58
+ private readonly transformUser;
59
+ constructor(options: JwtPrismaDriverOptions);
60
+ login(payload: AuthPayload, _req: Request, res: Response): Promise<User>;
61
+ checkIn(req: Request, res: Response): Promise<User | null>;
62
+ logout(_req: Request, res: Response): Promise<void>;
63
+ }
64
+ export type { JwtPrismaDriverOptions };
65
+ export { JwtPrismaCookieAuthDriver };
@@ -0,0 +1,5 @@
1
+ export * from "./types";
2
+ export * from "./token";
3
+ export * from "./AuthController";
4
+ export * from "./AuthModule";
5
+ export * from "./JwtPrismaCookieAuthDriver";
@@ -0,0 +1,2 @@
1
+ declare const AUTH_DRIVER: unique symbol;
2
+ export { AUTH_DRIVER };
@@ -0,0 +1,24 @@
1
+ import type { Request, Response } from "express";
2
+ export type AuthPayload = Record<string, any>;
3
+ export interface AuthResponse<User> {
4
+ message: string;
5
+ data: User;
6
+ }
7
+ export interface LogoutResponse {
8
+ message: string;
9
+ }
10
+ export interface AuthDriver<User = any> {
11
+ /**
12
+ * Deve validar o payload recebido e retornar o usuário autenticado.
13
+ */
14
+ login(payload: AuthPayload, request: Request, response: Response): Promise<User> | User;
15
+ /**
16
+ * Deve recuperar o usuário a partir do contexto (cookies, headers, sessão, etc.).
17
+ * Retorne `null` ou `undefined` quando não houver sessão ativa.
18
+ */
19
+ checkIn(request: Request, response: Response): Promise<User | null | undefined> | User | null | undefined;
20
+ /**
21
+ * Deve encerrar a sessão/tokens do usuário.
22
+ */
23
+ logout(request: Request, response: Response): Promise<void> | void;
24
+ }
@@ -0,0 +1,32 @@
1
+ import { Ref } from "vue";
2
+ import type { Fetch, FetchOptions, FetchResponse } from "@ministerjs/resource";
3
+ interface Options<User extends Record<string, any>> {
4
+ fetch: Fetch;
5
+ mapUser?: (user: User) => User;
6
+ routes?: {
7
+ signUp?: string;
8
+ login?: string;
9
+ checkIn?: string;
10
+ logout?: string;
11
+ };
12
+ afterLogout: () => void | Promise<void>;
13
+ afterCheckIn: (result: boolean) => void | Promise<void>;
14
+ }
15
+ declare class Auth<User extends Record<string, any>> {
16
+ user: Ref<Partial<User> | null>;
17
+ on: Ref<boolean, boolean>;
18
+ loading: Ref<boolean, boolean>;
19
+ checkedIn: Ref<boolean, boolean>;
20
+ private fetch;
21
+ private mapUser;
22
+ private afterLogout;
23
+ private afterCheckIn;
24
+ private routes;
25
+ constructor({ fetch, mapUser, routes, afterLogout, afterCheckIn, }: Options<User>);
26
+ login(payload: Record<string, any>): Promise<void>;
27
+ checkIn(): Promise<void>;
28
+ logout(): Promise<void>;
29
+ private setUser;
30
+ }
31
+ export type { Fetch, FetchOptions, FetchResponse };
32
+ export { Auth };
@@ -0,0 +1 @@
1
+ export * from "./VueAuth";