@robosophy/auth 1.1.0 → 1.2.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 +5 -13
- package/dist/index.d.ts +0 -2
- package/dist/index.js +4 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,6 @@ import { configureAuth } from "@robosophy/auth";
|
|
|
19
19
|
|
|
20
20
|
configureAuth({
|
|
21
21
|
landingUrl: import.meta.env.VITE_LANDING_URL, // origin del Landing (front y back comparten origin)
|
|
22
|
-
devEmail: import.meta.env.VITE_DEV_EMAIL, // solo DEV
|
|
23
22
|
isDev: import.meta.env.DEV,
|
|
24
23
|
});
|
|
25
24
|
```
|
|
@@ -30,7 +29,7 @@ configureAuth({
|
|
|
30
29
|
|
|
31
30
|
| Función | Qué hace |
|
|
32
31
|
|---|---|
|
|
33
|
-
| `configureAuth(opts)` | Guarda `landingUrl`, `
|
|
32
|
+
| `configureAuth(opts)` | Guarda `landingUrl`, `isDev` y `hostsLogin`. |
|
|
34
33
|
| `getToken()` | Devuelve `localStorage["token"]` o `null`. |
|
|
35
34
|
| `attachAuthRefresh(api)` | Agrega el Bearer a cada request y ante un 401 renueva el token y reintenta una vez. |
|
|
36
35
|
| `getCurrentUser()` | `GET {landingUrl}/api/usuario/self`, cacheado en memoria. Si falla, el cache se descarta. |
|
|
@@ -44,14 +43,12 @@ Ante un 401 de un request que todavía no se reintentó:
|
|
|
44
43
|
|
|
45
44
|
1. Se toma el lock `basf-auth-refresh` con `navigator.locks`, compartido entre pestañas. Si el navegador no lo soporta, se deduplica solo dentro de la pestaña.
|
|
46
45
|
2. Si `localStorage["token"]` ya cambió, otra pestaña renovó primero y se usa ese token.
|
|
47
|
-
3. Si no, se pide uno nuevo:
|
|
48
|
-
- En DEV con `devEmail`: `POST {landingUrl}/dev/token` con `{ email }`. El Landing back local lo expone solo en el perfil `local`.
|
|
49
|
-
- Si no: `POST {landingUrl}/api/auth/refresh` con `credentials: "include"`, que rota la cookie de refresh.
|
|
46
|
+
3. Si no, se pide uno nuevo: `POST {landingUrl}/api/auth/refresh` con `credentials: "include"`, que rota la cookie de refresh.
|
|
50
47
|
4. Con token nuevo se reintenta el request original. Si no hay token:
|
|
51
|
-
- En DEV: `console.warn(
|
|
48
|
+
- En DEV: `console.warn('[auth] sin sesión: copiá el token de dev en localStorage (key "token") y recargá')` y el error sigue. No redirige (salvo con `hostsLogin`).
|
|
52
49
|
- En prod, o con `hostsLogin`: `clearSession()` y redirect al login, salvo que ya esté en esa página.
|
|
53
50
|
|
|
54
|
-
Los 401 de `/api/auth/login`, `/api/auth/verify-2fa`, `/api/auth/resend-2fa`, `/api/auth/refresh
|
|
51
|
+
Los 401 de `/api/auth/login`, `/api/auth/verify-2fa`, `/api/auth/resend-2fa`, `/api/auth/refresh` y `/api/auth/logout` no disparan el refresh: ahí el 401 es la respuesta (credenciales, código 2FA, refresh vencido). El resto de `/api/auth/*`, como `request-2fa-challenge`, sí renueva y reintenta.
|
|
55
52
|
|
|
56
53
|
## Uso en Vue
|
|
57
54
|
|
|
@@ -61,7 +58,6 @@ import { configureAuth } from "@robosophy/auth";
|
|
|
61
58
|
|
|
62
59
|
configureAuth({
|
|
63
60
|
landingUrl: import.meta.env.VITE_LANDING_URL,
|
|
64
|
-
devEmail: import.meta.env.VITE_DEV_EMAIL,
|
|
65
61
|
isDev: import.meta.env.DEV,
|
|
66
62
|
});
|
|
67
63
|
```
|
|
@@ -110,7 +106,6 @@ import { configureAuth } from "@robosophy/auth";
|
|
|
110
106
|
|
|
111
107
|
configureAuth({
|
|
112
108
|
landingUrl: import.meta.env.VITE_LANDING_URL,
|
|
113
|
-
devEmail: import.meta.env.VITE_DEV_EMAIL,
|
|
114
109
|
isDev: import.meta.env.DEV,
|
|
115
110
|
});
|
|
116
111
|
```
|
|
@@ -137,7 +132,4 @@ import { logout } from "@robosophy/auth";
|
|
|
137
132
|
|
|
138
133
|
## Desarrollo local
|
|
139
134
|
|
|
140
|
-
|
|
141
|
-
- `VITE_DEV_EMAIL=usuario@basf.com` va en `.env.local`, que no se commitea.
|
|
142
|
-
- Con el Landing back en perfil `local`, el front pide el token solo en el primer 401: no hace falta loguearse ni copiar tokens.
|
|
143
|
-
- Para entrar con otro usuario se cambia el email y se recarga. `configureAuth` detecta el cambio (lo guarda en `localStorage["auth-dev-email"]`) y borra la sesión del usuario anterior.
|
|
135
|
+
En dev el access token dura 24 h. Para trabajar con un front local: loguearse en `https://dev.micuenta.basf.com.ar`, copiar `token` del `localStorage` y pegarlo en el `localStorage` del front local (`localhost:<puerto>`), con la misma key. Con `isDev` el paquete no redirige al login: si el token vence, avisa en la consola y hay que pegar uno nuevo.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
interface AuthOptions {
|
|
2
2
|
/** Origin del Landing (front y back comparten origin): ej. import.meta.env.VITE_LANDING_URL */
|
|
3
3
|
landingUrl: string;
|
|
4
|
-
/** Solo DEV: email para pedir el token a POST /dev/token del Landing back local. */
|
|
5
|
-
devEmail?: string;
|
|
6
4
|
/** import.meta.env.DEV */
|
|
7
5
|
isDev?: boolean;
|
|
8
6
|
/**
|
package/dist/index.js
CHANGED
|
@@ -4,17 +4,14 @@ var TOKEN_KEY = "token";
|
|
|
4
4
|
var SESSION_KEYS = [TOKEN_KEY, "user-id", "originalToken", "token-landing"];
|
|
5
5
|
var LOCK_NAME = "basf-auth-refresh";
|
|
6
6
|
var REFRESH_PATH = "/api/auth/refresh";
|
|
7
|
-
var DEV_TOKEN_PATH = "/dev/token";
|
|
8
7
|
var LOGOUT_PATH = "/api/auth/logout";
|
|
9
8
|
var SELF_PATH = "/api/usuario/self";
|
|
10
|
-
var DEV_EMAIL_KEY = "auth-dev-email";
|
|
11
9
|
var NO_RETRY_PATHS = [
|
|
12
10
|
"/api/auth/login",
|
|
13
11
|
"/api/auth/verify-2fa",
|
|
14
12
|
"/api/auth/resend-2fa",
|
|
15
13
|
REFRESH_PATH,
|
|
16
|
-
LOGOUT_PATH
|
|
17
|
-
DEV_TOKEN_PATH
|
|
14
|
+
LOGOUT_PATH
|
|
18
15
|
];
|
|
19
16
|
var options = null;
|
|
20
17
|
var landingApi = null;
|
|
@@ -23,16 +20,11 @@ var inFlightRefresh = null;
|
|
|
23
20
|
function configureAuth(opts) {
|
|
24
21
|
options = {
|
|
25
22
|
landingUrl: opts.landingUrl.replace(/\/+$/, ""),
|
|
26
|
-
devEmail: opts.devEmail ?? "",
|
|
27
23
|
isDev: opts.isDev ?? false,
|
|
28
24
|
hostsLogin: opts.hostsLogin ?? false
|
|
29
25
|
};
|
|
30
26
|
landingApi = null;
|
|
31
27
|
currentUser = null;
|
|
32
|
-
if (options.isDev && options.devEmail !== "" && localStorage.getItem(DEV_EMAIL_KEY) !== options.devEmail) {
|
|
33
|
-
clearSession();
|
|
34
|
-
localStorage.setItem(DEV_EMAIL_KEY, options.devEmail);
|
|
35
|
-
}
|
|
36
28
|
}
|
|
37
29
|
function getToken() {
|
|
38
30
|
return localStorage.getItem(TOKEN_KEY);
|
|
@@ -146,15 +138,9 @@ function withRefreshLock(task) {
|
|
|
146
138
|
return locks ? locks.request(LOCK_NAME, task) : task();
|
|
147
139
|
}
|
|
148
140
|
async function requestNewToken() {
|
|
149
|
-
const { landingUrl
|
|
150
|
-
const useDevToken = isDev && devEmail !== "";
|
|
141
|
+
const { landingUrl } = requireOptions();
|
|
151
142
|
try {
|
|
152
|
-
const response = await fetch(landingUrl +
|
|
153
|
-
method: "POST",
|
|
154
|
-
credentials: "include",
|
|
155
|
-
headers: useDevToken ? { "Content-Type": "application/json" } : void 0,
|
|
156
|
-
body: useDevToken ? JSON.stringify({ email: devEmail }) : void 0
|
|
157
|
-
});
|
|
143
|
+
const response = await fetch(landingUrl + REFRESH_PATH, { method: "POST", credentials: "include" });
|
|
158
144
|
if (!response.ok) {
|
|
159
145
|
return null;
|
|
160
146
|
}
|
|
@@ -167,7 +153,7 @@ async function requestNewToken() {
|
|
|
167
153
|
function onSessionLost() {
|
|
168
154
|
const { isDev, hostsLogin } = requireOptions();
|
|
169
155
|
if (isDev && !hostsLogin) {
|
|
170
|
-
console.warn(
|
|
156
|
+
console.warn('[auth] sin sesi\xF3n: copi\xE1 el token de dev en localStorage (key "token") y recarg\xE1');
|
|
171
157
|
return;
|
|
172
158
|
}
|
|
173
159
|
clearSession();
|