@arex95/vue-core 1.1.26 → 1.1.27
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/dist/composables/auth/useAuth.d.ts +5 -5
- package/dist/index.mjs +46 -19
- package/dist/types/Auth.d.ts +16 -0
- package/dist/types/index.d.ts +1 -3
- package/package.json +1 -1
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { AuthParams, AuthResponse } from "@/types";
|
|
1
|
+
import { AuthParams, AuthResponse, AuthTokenPaths } from "@/types";
|
|
2
2
|
import { SessionPreference } from "@config/global/sessionConfig";
|
|
3
3
|
/**
|
|
4
4
|
* @typedef {object} AuthHook
|
|
5
5
|
* @property {function(): Promise<number | null>} getTokenExpiry - Retrieves the expiration time of the current token in milliseconds.
|
|
6
6
|
* @property {function(): Promise<void>} cleanCredentials - Clears authentication credentials from storage.
|
|
7
7
|
* @property {(params?: AuthParams) => Promise<void>} logout - Logs out the user, clears credentials, and reloads the page.
|
|
8
|
-
* @property {(params: AuthParams, persistence: SessionPreference) => Promise<AuthResponse>} login - Logs in the user and stores tokens.
|
|
9
|
-
* @property {
|
|
8
|
+
* @property {(params: AuthParams, persistence: SessionPreference, tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>} login - Logs in the user and stores tokens.
|
|
9
|
+
* @property {(tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>} refresh - Refreshes authentication tokens.
|
|
10
10
|
* @property {function(): Promise<boolean>} verifyAuth - Verifies the validity and expiration of the current authentication token.
|
|
11
11
|
* @property {(preference: SessionPreference) => void} setSessionPersistencePreference - Sets the user's preferred storage for authentication data.
|
|
12
12
|
* @property {function(): SessionPreference} getSessionPersistencePreference - Retrieves the user's current preferred storage for authentication data.
|
|
@@ -19,6 +19,6 @@ import { SessionPreference } from "@config/global/sessionConfig";
|
|
|
19
19
|
*/
|
|
20
20
|
export declare function useAuth(secretKey?: string): {
|
|
21
21
|
logout: (params?: AuthParams) => Promise<void>;
|
|
22
|
-
login: (params: AuthParams, persistence: SessionPreference) => Promise<AuthResponse>;
|
|
23
|
-
refresh: () => Promise<AuthResponse>;
|
|
22
|
+
login: (params: AuthParams, persistence: SessionPreference, tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>;
|
|
23
|
+
refresh: (tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>;
|
|
24
24
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -3305,8 +3305,8 @@ function useSorter(items, criteriaList, selectedCriteria) {
|
|
|
3305
3305
|
* @property {function(): Promise<number | null>} getTokenExpiry - Retrieves the expiration time of the current token in milliseconds.
|
|
3306
3306
|
* @property {function(): Promise<void>} cleanCredentials - Clears authentication credentials from storage.
|
|
3307
3307
|
* @property {(params?: AuthParams) => Promise<void>} logout - Logs out the user, clears credentials, and reloads the page.
|
|
3308
|
-
* @property {(params: AuthParams, persistence: SessionPreference) => Promise<AuthResponse>} login - Logs in the user and stores tokens.
|
|
3309
|
-
* @property {
|
|
3308
|
+
* @property {(params: AuthParams, persistence: SessionPreference, tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>} login - Logs in the user and stores tokens.
|
|
3309
|
+
* @property {(tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>} refresh - Refreshes authentication tokens.
|
|
3310
3310
|
* @property {function(): Promise<boolean>} verifyAuth - Verifies the validity and expiration of the current authentication token.
|
|
3311
3311
|
* @property {(preference: SessionPreference) => void} setSessionPersistencePreference - Sets the user's preferred storage for authentication data.
|
|
3312
3312
|
* @property {function(): SessionPreference} getSessionPersistencePreference - Retrieves the user's current preferred storage for authentication data.
|
|
@@ -3347,26 +3347,33 @@ function useAuth(secretKey = getSecretKey()) {
|
|
|
3347
3347
|
*
|
|
3348
3348
|
* @param {AuthParams} params - The authentication parameters (e.g., username, password).
|
|
3349
3349
|
* @param {SessionPreference} persistence - The storage preference: 'local' for localStorage, 'session' for sessionStorage.
|
|
3350
|
-
* @
|
|
3351
|
-
* @
|
|
3350
|
+
* @param {AuthTokenPaths} [tokenPaths] - Configuración opcional para las rutas (en notación de punto) donde se encuentran los tokens en la respuesta de la API.
|
|
3351
|
+
* @returns {Promise<AuthResponse>} La respuesta de autenticación que contiene los tokens e información del usuario.
|
|
3352
|
+
* @throws {Error} Si la solicitud de login falla, o si los tokens de acceso/refresco no se encuentran o no son válidos en la respuesta.
|
|
3352
3353
|
*/
|
|
3353
|
-
const login = async (params, persistence) => {
|
|
3354
|
+
const login = async (params, persistence, tokenPaths) => {
|
|
3354
3355
|
try {
|
|
3355
3356
|
const { data } = await axiosInstance.post(endpoints.LOGIN, params);
|
|
3357
|
+
const accessTokenPath = tokenPaths?.accessTokenPath || "access_token";
|
|
3358
|
+
const refreshTokenPath = tokenPaths?.refreshTokenPath || "refresh_token";
|
|
3359
|
+
const accessTokenPathArray = accessTokenPath.split(".");
|
|
3360
|
+
const refreshTokenPathArray = refreshTokenPath.split(".");
|
|
3356
3361
|
if (!data) {
|
|
3357
3362
|
throw new Error("LOGIN_ERROR: No data received from login endpoint.");
|
|
3358
3363
|
}
|
|
3359
|
-
|
|
3360
|
-
|
|
3364
|
+
const accessToken = safeGet(data, accessTokenPathArray);
|
|
3365
|
+
const refreshToken = safeGet(data, refreshTokenPathArray);
|
|
3366
|
+
if (!accessToken || typeof accessToken !== "string") {
|
|
3367
|
+
throw new Error(`LOGIN_ERROR: Access token not found or invalid at path '${accessTokenPath}' in response.`);
|
|
3361
3368
|
}
|
|
3362
|
-
if (!
|
|
3363
|
-
throw new Error(
|
|
3369
|
+
if (!refreshToken || typeof refreshToken !== "string") {
|
|
3370
|
+
throw new Error(`LOGIN_ERROR: Refresh token not found or invalid at path '${refreshTokenPath}' in response.`);
|
|
3364
3371
|
}
|
|
3365
3372
|
configSession({
|
|
3366
3373
|
persistencePreference: persistence,
|
|
3367
3374
|
});
|
|
3368
|
-
await storeAuthToken(
|
|
3369
|
-
await storeAuthRefreshToken(
|
|
3375
|
+
await storeAuthToken(accessToken, secretKey, persistence);
|
|
3376
|
+
await storeAuthRefreshToken(refreshToken, secretKey, persistence);
|
|
3370
3377
|
return data;
|
|
3371
3378
|
}
|
|
3372
3379
|
catch (error) {
|
|
@@ -3376,20 +3383,40 @@ function useAuth(secretKey = getSecretKey()) {
|
|
|
3376
3383
|
};
|
|
3377
3384
|
/**
|
|
3378
3385
|
* Refreshes the authentication tokens using the stored refresh token.
|
|
3386
|
+
* This function can also accept optional token paths if the refresh endpoint
|
|
3387
|
+
* returns tokens with a different structure than the default login.
|
|
3379
3388
|
* If no refresh token is found, it throws an error and initiates a logout.
|
|
3380
3389
|
*
|
|
3390
|
+
* @param {AuthTokenPaths} [tokenPaths] - Configuración opcional para las rutas (en notación de punto) de los tokens de acceso y refresco en la respuesta del endpoint de refresco.
|
|
3381
3391
|
* @returns {Promise<AuthResponse>} The new authentication response with refreshed tokens.
|
|
3382
3392
|
* @throws {Error} If the refresh token is missing or the refresh request fails.
|
|
3383
3393
|
*/
|
|
3384
|
-
const refresh = async () => {
|
|
3394
|
+
const refresh = async (tokenPaths) => {
|
|
3385
3395
|
try {
|
|
3386
|
-
const
|
|
3387
|
-
if (!
|
|
3388
|
-
throw new Error("TOKEN_MISSING: No refresh token found");
|
|
3396
|
+
const refreshTokenFromStorage = await getAuthRefreshToken(secretKey, currentPersistencePreference);
|
|
3397
|
+
if (!refreshTokenFromStorage) {
|
|
3398
|
+
throw new Error("TOKEN_MISSING: No refresh token found in storage.");
|
|
3389
3399
|
}
|
|
3390
|
-
const { data } = await axiosInstance.post(endpoints.REFRESH, { refresh_token:
|
|
3391
|
-
|
|
3392
|
-
|
|
3400
|
+
const { data } = await axiosInstance.post(endpoints.REFRESH, { refresh_token: refreshTokenFromStorage });
|
|
3401
|
+
const accessTokenPath = tokenPaths?.accessTokenPath || "access_token";
|
|
3402
|
+
const refreshTokenPath = tokenPaths?.refreshTokenPath || "refresh_token";
|
|
3403
|
+
const accessTokenPathArray = accessTokenPath.split(".");
|
|
3404
|
+
const refreshTokenPathArray = refreshTokenPath.split(".");
|
|
3405
|
+
if (!data) {
|
|
3406
|
+
throw new Error("REFRESH_ERROR: No data received from refresh endpoint.");
|
|
3407
|
+
}
|
|
3408
|
+
const accessTokenAfterRefresh = safeGet(data, accessTokenPathArray);
|
|
3409
|
+
const refreshTokenAfterRefresh = safeGet(data, refreshTokenPathArray);
|
|
3410
|
+
if (!accessTokenAfterRefresh ||
|
|
3411
|
+
typeof accessTokenAfterRefresh !== "string") {
|
|
3412
|
+
throw new Error(`REFRESH_ERROR: Access token not found or invalid at path '${accessTokenPath}' in refresh response.`);
|
|
3413
|
+
}
|
|
3414
|
+
if (!refreshTokenAfterRefresh ||
|
|
3415
|
+
typeof refreshTokenAfterRefresh !== "string") {
|
|
3416
|
+
throw new Error(`REFRESH_ERROR: Refresh token not found or invalid at path '${refreshTokenPath}' in refresh response.`);
|
|
3417
|
+
}
|
|
3418
|
+
await storeAuthToken(accessTokenAfterRefresh, secretKey, currentPersistencePreference);
|
|
3419
|
+
await storeAuthRefreshToken(refreshTokenAfterRefresh, secretKey, currentPersistencePreference);
|
|
3393
3420
|
return data;
|
|
3394
3421
|
}
|
|
3395
3422
|
catch (error) {
|
|
@@ -3401,7 +3428,7 @@ function useAuth(secretKey = getSecretKey()) {
|
|
|
3401
3428
|
return {
|
|
3402
3429
|
logout,
|
|
3403
3430
|
login,
|
|
3404
|
-
refresh
|
|
3431
|
+
refresh,
|
|
3405
3432
|
};
|
|
3406
3433
|
}
|
|
3407
3434
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TokensConfig, EndpointsConfig } from "@/types";
|
|
2
|
+
export type AuthConfig = {
|
|
3
|
+
endpoints: EndpointsConfig;
|
|
4
|
+
storageKeys: TokensConfig;
|
|
5
|
+
};
|
|
6
|
+
export interface AuthParams {
|
|
7
|
+
username?: string;
|
|
8
|
+
password?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface AuthTokenPaths {
|
|
11
|
+
accessTokenPath?: string;
|
|
12
|
+
refreshTokenPath?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AuthResponse {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,9 +3,7 @@ export * from './ExtendedQueryOptions';
|
|
|
3
3
|
export * from './ErrorType';
|
|
4
4
|
export * from './EndpointsConfig';
|
|
5
5
|
export * from './TokenConfig';
|
|
6
|
-
export * from './
|
|
6
|
+
export * from './Auth';
|
|
7
7
|
export * from './SessionConfig';
|
|
8
8
|
export * from './DecodedJwtPayload';
|
|
9
|
-
export * from './AuthResponse';
|
|
10
|
-
export * from './AuthParams';
|
|
11
9
|
export * from './ArexVueCoreOptions';
|