@ministerjs/auth 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.
- package/README.md +121 -0
- package/dist/Auth.d.mts +51 -0
- package/dist/Auth.d.ts +51 -0
- package/dist/Auth.js +116 -0
- package/dist/Auth.mjs +91 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# @ministerjs/auth
|
|
2
|
+
|
|
3
|
+
Classe para gerenciar autenticação de usuários em aplicações Vue.
|
|
4
|
+
|
|
5
|
+
## Instalação
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @ministerjs/auth
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Importação
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Auth } from "@ministerjs/auth/Auth";
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Exemplo de Uso
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Auth } from "@ministerjs/auth/Auth";
|
|
22
|
+
|
|
23
|
+
const auth = new Auth({
|
|
24
|
+
fetch: window.fetch.bind(window),
|
|
25
|
+
mapUser: (user) => {
|
|
26
|
+
// Transformar ou filtrar dados do usuário antes de salvar
|
|
27
|
+
return {
|
|
28
|
+
...user,
|
|
29
|
+
fullName: `${user.firstName} ${user.lastName}`,
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
routes: {
|
|
33
|
+
login: "/api/login",
|
|
34
|
+
checkIn: "/api/checkin",
|
|
35
|
+
logout: "/api/logout",
|
|
36
|
+
},
|
|
37
|
+
afterLogout: () => {
|
|
38
|
+
// Ações após fazer logout
|
|
39
|
+
console.log("Usuário deslogado!");
|
|
40
|
+
},
|
|
41
|
+
afterCheckIn: (result) => {
|
|
42
|
+
// Ações após verificação do estado de login
|
|
43
|
+
console.log("CheckIn foi bem-sucedido?", result);
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Atributos Importantes
|
|
49
|
+
|
|
50
|
+
- **`auth.user`**: contém os dados do usuário autenticado (ou `null` se não autenticado).
|
|
51
|
+
- **`auth.on`**: booleano que indica se o usuário está logado (`true`/`false`).
|
|
52
|
+
- **`auth.loading`**: booleano que indica se há uma operação de login, checkIn ou logout em andamento.
|
|
53
|
+
- **`auth.checkedIn`**: booleano que indica se o `checkIn()` já foi executado.
|
|
54
|
+
|
|
55
|
+
### Métodos
|
|
56
|
+
|
|
57
|
+
#### `login(payload: Record<string, any>)`
|
|
58
|
+
|
|
59
|
+
- Faz a chamada de login para a rota configurada em `routes.login`.
|
|
60
|
+
- Ao receber resposta:
|
|
61
|
+
- Define `auth.on` como `true`.
|
|
62
|
+
- Armazena o usuário em `auth.user`.
|
|
63
|
+
- Define `auth.loading` como `false`.
|
|
64
|
+
|
|
65
|
+
**Exemplo**:
|
|
66
|
+
```ts
|
|
67
|
+
await auth.login({ username: "john", password: "1234" });
|
|
68
|
+
console.log(auth.user.value); // Dados do usuário logado
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### `checkIn()`
|
|
72
|
+
|
|
73
|
+
- Verifica se o usuário já está autenticado (ex.: mantém a sessão em abas novas).
|
|
74
|
+
- Atualiza `auth.user` e `auth.on` conforme o resultado.
|
|
75
|
+
- Chama o callback `afterCheckIn(true|false)` dependendo do sucesso ou falha na verificação.
|
|
76
|
+
- Atualiza `auth.checkedIn` para `true` quando finaliza.
|
|
77
|
+
|
|
78
|
+
**Exemplo**:
|
|
79
|
+
```ts
|
|
80
|
+
await auth.checkIn();
|
|
81
|
+
console.log(auth.on.value); // true ou false
|
|
82
|
+
console.log(auth.checkedIn.value); // true
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### `logout()`
|
|
86
|
+
|
|
87
|
+
- Faz a chamada de logout para a rota configurada em `routes.logout`.
|
|
88
|
+
- Define `auth.on` como `false`, limpa `auth.user` e chama `afterLogout()` após concluir.
|
|
89
|
+
|
|
90
|
+
**Exemplo**:
|
|
91
|
+
```ts
|
|
92
|
+
await auth.logout();
|
|
93
|
+
console.log(auth.on.value); // false
|
|
94
|
+
console.log(auth.user.value); // null
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Opções do Construtor
|
|
98
|
+
|
|
99
|
+
- **`fetch: Fetch`**
|
|
100
|
+
Instância de Fetch responsável pelas requisições HTTP.
|
|
101
|
+
|
|
102
|
+
- **`mapUser?: (user: User) => User`**
|
|
103
|
+
Callback para ajustar dados do usuário antes de armazenar em `auth.user`.
|
|
104
|
+
|
|
105
|
+
- **`routes?: { login?: string; checkIn?: string; logout?: string; }`**
|
|
106
|
+
Rotas customizadas para as operações de login, checkIn e logout.
|
|
107
|
+
|
|
108
|
+
- **`afterLogout?: () => void | Promise<void>`**
|
|
109
|
+
Executado logo após o logout.
|
|
110
|
+
|
|
111
|
+
- **`afterCheckIn?: (result: boolean) => void | Promise<void>`**
|
|
112
|
+
Executado após a tentativa de checkIn, recebendo `true` ou `false` como resultado.
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Rotas do Backend
|
|
116
|
+
|
|
117
|
+
Para que a classe Auth funcione corretamente, o backend deve expor as rotas (por padrão: `/api/login`, `/api/checkin`, `/api/logout`).
|
|
118
|
+
|
|
119
|
+
- **Login**: recebe as credenciais no corpo da requisição, valida e retorna `{ message, data }`.
|
|
120
|
+
- **CheckIn**: verifica a sessão e retorna `{ message, data }` se o usuário estiver autenticado, ou algum erro/status caso não esteja.
|
|
121
|
+
- **Logout**: invalida a sessão/tokens e retorna `{ message }`.
|
package/dist/Auth.d.mts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
type Methods = "get" | "post" | "put" | "patch" | "delete";
|
|
4
|
+
|
|
5
|
+
interface FetchOptions<T> {
|
|
6
|
+
method?: Methods | Uppercase<Methods>;
|
|
7
|
+
body?: RequestInit["body"];
|
|
8
|
+
query?: Record<string, any>;
|
|
9
|
+
transform?: (data: any) => T;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface FetchResponse<T> {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
json(): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Fetch = <T>(
|
|
18
|
+
url: string,
|
|
19
|
+
options?: FetchOptions<T>,
|
|
20
|
+
) => Promise<FetchResponse<T>>;
|
|
21
|
+
|
|
22
|
+
interface Options<User extends Record<string, any>> {
|
|
23
|
+
fetch: Fetch;
|
|
24
|
+
mapUser?: (user: User) => User;
|
|
25
|
+
routes?: {
|
|
26
|
+
signUp?: string;
|
|
27
|
+
login?: string;
|
|
28
|
+
checkIn?: string;
|
|
29
|
+
logout?: string;
|
|
30
|
+
};
|
|
31
|
+
afterLogout: () => void | Promise<void>;
|
|
32
|
+
afterCheckIn: (result: boolean) => void | Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
declare class Auth<User extends Record<string, any>> {
|
|
35
|
+
user: Ref<Partial<User> | null>;
|
|
36
|
+
on: Ref<boolean, boolean>;
|
|
37
|
+
loading: Ref<boolean, boolean>;
|
|
38
|
+
checkedIn: Ref<boolean, boolean>;
|
|
39
|
+
private fetch;
|
|
40
|
+
private mapUser;
|
|
41
|
+
private afterLogout;
|
|
42
|
+
private afterCheckIn;
|
|
43
|
+
private routes;
|
|
44
|
+
constructor({ fetch, mapUser, routes, afterLogout, afterCheckIn, }: Options<User>);
|
|
45
|
+
login(payload: Record<string, any>): Promise<void>;
|
|
46
|
+
checkIn(): Promise<void>;
|
|
47
|
+
logout(): Promise<void>;
|
|
48
|
+
private setUser;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { Auth, type Fetch, type FetchOptions, type FetchResponse };
|
package/dist/Auth.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Ref } from 'vue';
|
|
2
|
+
|
|
3
|
+
type Methods = "get" | "post" | "put" | "patch" | "delete";
|
|
4
|
+
|
|
5
|
+
interface FetchOptions<T> {
|
|
6
|
+
method?: Methods | Uppercase<Methods>;
|
|
7
|
+
body?: RequestInit["body"];
|
|
8
|
+
query?: Record<string, any>;
|
|
9
|
+
transform?: (data: any) => T;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface FetchResponse<T> {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
json(): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type Fetch = <T>(
|
|
18
|
+
url: string,
|
|
19
|
+
options?: FetchOptions<T>,
|
|
20
|
+
) => Promise<FetchResponse<T>>;
|
|
21
|
+
|
|
22
|
+
interface Options<User extends Record<string, any>> {
|
|
23
|
+
fetch: Fetch;
|
|
24
|
+
mapUser?: (user: User) => User;
|
|
25
|
+
routes?: {
|
|
26
|
+
signUp?: string;
|
|
27
|
+
login?: string;
|
|
28
|
+
checkIn?: string;
|
|
29
|
+
logout?: string;
|
|
30
|
+
};
|
|
31
|
+
afterLogout: () => void | Promise<void>;
|
|
32
|
+
afterCheckIn: (result: boolean) => void | Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
declare class Auth<User extends Record<string, any>> {
|
|
35
|
+
user: Ref<Partial<User> | null>;
|
|
36
|
+
on: Ref<boolean, boolean>;
|
|
37
|
+
loading: Ref<boolean, boolean>;
|
|
38
|
+
checkedIn: Ref<boolean, boolean>;
|
|
39
|
+
private fetch;
|
|
40
|
+
private mapUser;
|
|
41
|
+
private afterLogout;
|
|
42
|
+
private afterCheckIn;
|
|
43
|
+
private routes;
|
|
44
|
+
constructor({ fetch, mapUser, routes, afterLogout, afterCheckIn, }: Options<User>);
|
|
45
|
+
login(payload: Record<string, any>): Promise<void>;
|
|
46
|
+
checkIn(): Promise<void>;
|
|
47
|
+
logout(): Promise<void>;
|
|
48
|
+
private setUser;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { Auth, type Fetch, type FetchOptions, type FetchResponse };
|
package/dist/Auth.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/Auth.ts
|
|
21
|
+
var Auth_exports = {};
|
|
22
|
+
__export(Auth_exports, {
|
|
23
|
+
Auth: () => Auth
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Auth_exports);
|
|
26
|
+
var import_vue = require("vue");
|
|
27
|
+
var Auth = class {
|
|
28
|
+
user = (0, import_vue.ref)(null);
|
|
29
|
+
on = (0, import_vue.ref)(false);
|
|
30
|
+
loading = (0, import_vue.ref)(false);
|
|
31
|
+
checkedIn = (0, import_vue.ref)(false);
|
|
32
|
+
fetch;
|
|
33
|
+
mapUser;
|
|
34
|
+
afterLogout;
|
|
35
|
+
afterCheckIn;
|
|
36
|
+
routes = {
|
|
37
|
+
login: "/api/login",
|
|
38
|
+
checkIn: "/api/checkin",
|
|
39
|
+
logout: "/api/logout"
|
|
40
|
+
};
|
|
41
|
+
constructor({
|
|
42
|
+
fetch,
|
|
43
|
+
mapUser,
|
|
44
|
+
routes,
|
|
45
|
+
afterLogout,
|
|
46
|
+
afterCheckIn
|
|
47
|
+
}) {
|
|
48
|
+
this.fetch = fetch;
|
|
49
|
+
this.mapUser = mapUser ?? ((user) => user);
|
|
50
|
+
this.afterLogout = afterLogout ?? (() => {
|
|
51
|
+
});
|
|
52
|
+
this.afterCheckIn = afterCheckIn ?? (() => {
|
|
53
|
+
});
|
|
54
|
+
if (routes) {
|
|
55
|
+
Object.assign(this.routes, routes);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async login(payload) {
|
|
59
|
+
this.loading.value = true;
|
|
60
|
+
const response = await this.fetch(this.routes.login, {
|
|
61
|
+
method: "POST",
|
|
62
|
+
body: JSON.stringify(payload)
|
|
63
|
+
});
|
|
64
|
+
this.on.value = true;
|
|
65
|
+
const { data } = await response.json();
|
|
66
|
+
this.setUser(data);
|
|
67
|
+
this.loading.value = false;
|
|
68
|
+
}
|
|
69
|
+
async checkIn() {
|
|
70
|
+
this.loading.value = true;
|
|
71
|
+
try {
|
|
72
|
+
const response = await this.fetch(this.routes.checkIn, {
|
|
73
|
+
method: "POST"
|
|
74
|
+
});
|
|
75
|
+
const { data } = await response.json();
|
|
76
|
+
this.setUser(data);
|
|
77
|
+
this.on.value = true;
|
|
78
|
+
this.afterCheckIn(true);
|
|
79
|
+
} catch {
|
|
80
|
+
this.afterCheckIn(false);
|
|
81
|
+
} finally {
|
|
82
|
+
this.loading.value = false;
|
|
83
|
+
this.checkedIn.value = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async logout() {
|
|
87
|
+
this.loading.value = true;
|
|
88
|
+
try {
|
|
89
|
+
await this.fetch(this.routes.logout, {
|
|
90
|
+
method: "POST"
|
|
91
|
+
});
|
|
92
|
+
this.on.value = false;
|
|
93
|
+
this.setUser(null);
|
|
94
|
+
await this.afterLogout();
|
|
95
|
+
} finally {
|
|
96
|
+
this.loading.value = false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
setUser = (data) => {
|
|
100
|
+
if (data === null || data === void 0) {
|
|
101
|
+
this.user.value = null;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const parsedData = this.mapUser(data);
|
|
105
|
+
if (this.user.value === null) {
|
|
106
|
+
this.user.value = {};
|
|
107
|
+
}
|
|
108
|
+
for (const key in parsedData) {
|
|
109
|
+
this.user.value[key] = parsedData[key];
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
114
|
+
0 && (module.exports = {
|
|
115
|
+
Auth
|
|
116
|
+
});
|
package/dist/Auth.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// src/Auth.ts
|
|
2
|
+
import { ref } from "vue";
|
|
3
|
+
var Auth = class {
|
|
4
|
+
user = ref(null);
|
|
5
|
+
on = ref(false);
|
|
6
|
+
loading = ref(false);
|
|
7
|
+
checkedIn = ref(false);
|
|
8
|
+
fetch;
|
|
9
|
+
mapUser;
|
|
10
|
+
afterLogout;
|
|
11
|
+
afterCheckIn;
|
|
12
|
+
routes = {
|
|
13
|
+
login: "/api/login",
|
|
14
|
+
checkIn: "/api/checkin",
|
|
15
|
+
logout: "/api/logout"
|
|
16
|
+
};
|
|
17
|
+
constructor({
|
|
18
|
+
fetch,
|
|
19
|
+
mapUser,
|
|
20
|
+
routes,
|
|
21
|
+
afterLogout,
|
|
22
|
+
afterCheckIn
|
|
23
|
+
}) {
|
|
24
|
+
this.fetch = fetch;
|
|
25
|
+
this.mapUser = mapUser ?? ((user) => user);
|
|
26
|
+
this.afterLogout = afterLogout ?? (() => {
|
|
27
|
+
});
|
|
28
|
+
this.afterCheckIn = afterCheckIn ?? (() => {
|
|
29
|
+
});
|
|
30
|
+
if (routes) {
|
|
31
|
+
Object.assign(this.routes, routes);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async login(payload) {
|
|
35
|
+
this.loading.value = true;
|
|
36
|
+
const response = await this.fetch(this.routes.login, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
body: JSON.stringify(payload)
|
|
39
|
+
});
|
|
40
|
+
this.on.value = true;
|
|
41
|
+
const { data } = await response.json();
|
|
42
|
+
this.setUser(data);
|
|
43
|
+
this.loading.value = false;
|
|
44
|
+
}
|
|
45
|
+
async checkIn() {
|
|
46
|
+
this.loading.value = true;
|
|
47
|
+
try {
|
|
48
|
+
const response = await this.fetch(this.routes.checkIn, {
|
|
49
|
+
method: "POST"
|
|
50
|
+
});
|
|
51
|
+
const { data } = await response.json();
|
|
52
|
+
this.setUser(data);
|
|
53
|
+
this.on.value = true;
|
|
54
|
+
this.afterCheckIn(true);
|
|
55
|
+
} catch {
|
|
56
|
+
this.afterCheckIn(false);
|
|
57
|
+
} finally {
|
|
58
|
+
this.loading.value = false;
|
|
59
|
+
this.checkedIn.value = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async logout() {
|
|
63
|
+
this.loading.value = true;
|
|
64
|
+
try {
|
|
65
|
+
await this.fetch(this.routes.logout, {
|
|
66
|
+
method: "POST"
|
|
67
|
+
});
|
|
68
|
+
this.on.value = false;
|
|
69
|
+
this.setUser(null);
|
|
70
|
+
await this.afterLogout();
|
|
71
|
+
} finally {
|
|
72
|
+
this.loading.value = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
setUser = (data) => {
|
|
76
|
+
if (data === null || data === void 0) {
|
|
77
|
+
this.user.value = null;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const parsedData = this.mapUser(data);
|
|
81
|
+
if (this.user.value === null) {
|
|
82
|
+
this.user.value = {};
|
|
83
|
+
}
|
|
84
|
+
for (const key in parsedData) {
|
|
85
|
+
this.user.value[key] = parsedData[key];
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
export {
|
|
90
|
+
Auth
|
|
91
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ministerjs/auth",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "UNLICENSED",
|
|
5
|
+
"private": false,
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/*"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./Auth": {
|
|
14
|
+
"types": "./dist/Auth.d.ts",
|
|
15
|
+
"import": "./dist/Auth.mjs",
|
|
16
|
+
"require": "./dist/Auth.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"vue": "^3.5.12"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@eslint/js": "^9.14.0",
|
|
24
|
+
"@ministerjs/build": "^2.0.2",
|
|
25
|
+
"@types/node": "^22.13.5",
|
|
26
|
+
"cross-env": "^7.0.3",
|
|
27
|
+
"eslint": "^9.22.0",
|
|
28
|
+
"eslint-config-prettier": "^10.1.1",
|
|
29
|
+
"globals": "^15.12.0",
|
|
30
|
+
"prettier": "3.5.3",
|
|
31
|
+
"tsup": "^8.4.0",
|
|
32
|
+
"tsx": "^4.19.3",
|
|
33
|
+
"typescript": "latest",
|
|
34
|
+
"typescript-eslint": "^8.12.2",
|
|
35
|
+
"vue": "^3.5.12"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"dev": "tsup --watch",
|
|
39
|
+
"lint": "eslint ./src --fix",
|
|
40
|
+
"build": "pnpm lint && tsup",
|
|
41
|
+
"release": "pnpm publish --no-git-checks"
|
|
42
|
+
}
|
|
43
|
+
}
|