@arex95/vue-core 3.3.0 → 5.1.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.
@@ -1,51 +1,32 @@
1
- import { LocationPreference } from "@/types/SessionConfig";
1
+ import { LocationPreference } from '@/types/SessionConfig';
2
2
  /**
3
- * Removes all stored authentication credentials (access and refresh tokens) from the specified storage locations.
3
+ * Removes all stored authentication credentials (access + refresh tokens)
4
+ * from the specified storage location(s).
4
5
  *
5
- * @param {LocationPreference} location - The storage location to clear. Can be 'local' for `localStorage`,
6
- * 'session' for `sessionStorage`, 'cookie' for cookies, or 'any' to clear all.
7
- * @returns {Promise<void>} A promise that resolves when the credentials have been cleared.
6
+ * With location='any', ALL storage locations are cleared including cookies.
7
+ * This prevents orphaned tokens surviving a logout.
8
8
  */
9
9
  export declare const cleanCredentials: (location: LocationPreference) => Promise<void>;
10
10
  /**
11
11
  * Retrieves and decrypts the access token from the specified storage location.
12
- *
13
- * @param {string} secretKey - The secret key to use for decryption.
14
- * @param {LocationPreference} location - The storage location to search ('local', 'session', 'cookie', or 'any').
15
- * @returns {Promise<string | null>} A promise that resolves with the decrypted access token, or `null` if it's not found.
16
12
  */
17
13
  export declare const getAuthToken: (secretKey: string, location: LocationPreference) => Promise<string | null>;
18
14
  /**
19
15
  * Retrieves and decrypts the refresh token from the specified storage location.
20
- *
21
- * @param {string} secretKey - The secret key to use for decryption.
22
- * @param {LocationPreference} location - The storage location to search ('local', 'session', 'cookie', or 'any').
23
- * @returns {Promise<string | null>} A promise that resolves with the decrypted refresh token, or `null` if it's not found.
24
16
  */
25
17
  export declare const getAuthRefreshToken: (secretKey: string, location: LocationPreference) => Promise<string | null>;
26
18
  /**
27
- * Encrypts and stores the access token in the specified storage location.
28
- *
29
- * @param {string} token - The access token to store.
30
- * @param {string} secretKey - The secret key to use for encryption.
31
- * @param {LocationPreference} location - The storage location ('local', 'session', or 'cookie').
32
- * @returns {Promise<void>} A promise that resolves when the token has been stored.
19
+ * Encrypts and stores the access token.
33
20
  */
34
21
  export declare const storeAuthToken: (token: string, secretKey: string, location: LocationPreference) => Promise<void>;
35
22
  /**
36
- * Encrypts and stores the refresh token in the specified storage location.
37
- *
38
- * @param {string} token - The refresh token to store.
39
- * @param {string} secretKey - The secret key to use for encryption.
40
- * @param {LocationPreference} location - The storage location ('local', 'session', or 'cookie').
41
- * @returns {Promise<void>} A promise that resolves when the token has been stored.
23
+ * Encrypts and stores the refresh token.
42
24
  */
43
25
  export declare const storeAuthRefreshToken: (token: string, secretKey: string, location: LocationPreference) => Promise<void>;
44
26
  /**
45
- * Verifies the current user's authentication status by checking for a valid, unexpired access token.
46
- * It searches for the token in all storage locations (sessionStorage, localStorage, cookies).
47
- * If the token is missing, malformed, or expired, it logs the issue, clears credentials, and returns `false`.
27
+ * Verifies the current user's authentication status by checking for a
28
+ * valid, unexpired access token across all storage locations.
48
29
  *
49
- * @returns {Promise<boolean>} A promise that resolves to `true` if the user is authenticated, and `false` otherwise.
30
+ * Returns false (without throwing) if the token is missing, malformed, or expired.
50
31
  */
51
32
  export declare const verifyAuth: () => Promise<boolean>;
@@ -1,12 +1,15 @@
1
- import { AuthResponse, Fetcher } from "@/types";
1
+ import { AuthResponse, Fetcher } from '@/types';
2
2
  /**
3
- * Refreshes the access and refresh tokens by making a POST request to the refresh endpoint.
4
- * It retrieves the current refresh token from storage, sends it to the refresh endpoint,
5
- * and then stores the new tokens upon a successful response. If the refresh process fails
6
- * or no refresh token is found, it clears all credentials and reloads the page.
3
+ * Refreshes the access and refresh tokens.
7
4
  *
8
- * @param {Fetcher} [fetcher] - Optional fetcher function to use for the refresh request. If not provided, uses the default configured fetcher.
9
- * @returns {Promise<AuthResponse>} A promise that resolves with the new authentication response containing the refreshed tokens.
10
- * @throws {Error} Throws an error if the refresh token is missing or if the refresh request fails, which is then caught to trigger a logout.
5
+ * Fixes over the original:
6
+ * 1. Uses `persistence` (the location where tokens were originally stored)
7
+ * instead of hardcoding `"any"` so cookies / localStorage / sessionStorage
8
+ * are searched in the right place.
9
+ * 2. Sends the refresh token in the request body using the configured
10
+ * `refreshTokenPaths.refreshTokenPath` as the body key, so the backend
11
+ * always receives it regardless of withCredentials or cookie settings.
12
+ *
13
+ * On failure: clears credentials and calls `onRefreshFailed` callback.
11
14
  */
12
15
  export declare const refreshTokens: (fetcher?: Fetcher) => Promise<AuthResponse>;
@@ -1,44 +1,24 @@
1
1
  /**
2
- * Converts an `ArrayBuffer` or `Uint8Array` into a hexadecimal string representation.
3
- *
4
- * @param {ArrayBuffer | Uint8Array} buffer - The buffer to convert.
5
- * @returns {string} The resulting hexadecimal string.
2
+ * Converts an `ArrayBuffer` or `Uint8Array` into a hexadecimal string.
6
3
  */
7
4
  export declare function ab2hex(buffer: ArrayBuffer | Uint8Array): string;
8
5
  /**
9
6
  * Converts a hexadecimal string into a `Uint8Array`.
10
- *
11
- * @param {string} hex - The hexadecimal string to convert.
12
- * @returns {Uint8Array} The resulting `Uint8Array`.
13
- * @throws {TypeError} If the input is not a string.
14
- * @throws {Error} If the hexadecimal string has an invalid format or an odd length.
15
7
  */
16
8
  export declare function hex2ab(hex: string): Uint8Array;
17
9
  /**
18
- * Derives a `CryptoKey` for AES-CBC encryption from a plain-text secret key.
19
- * It uses SHA-256 to hash the secret key, ensuring a fixed-length key suitable for the Web Crypto API.
10
+ * Derives a CryptoKey for AES-CBC-256 from a plain-text secret using SHA-256.
20
11
  *
21
- * @param {string} secretKey - The plain-text secret key.
22
- * @returns {Promise<CryptoKey>} A promise that resolves with the derived `CryptoKey`.
23
- * @throws {Error} If the `secretKey` is null or empty.
12
+ * @throws {Error} If secretKey is empty or Web Crypto API is unavailable.
24
13
  */
25
14
  export declare function importKey(secretKey: string): Promise<CryptoKey>;
26
15
  /**
27
- * Encrypts a plain-text value using AES-CBC with a given secret key.
28
- * A random 16-byte initialization vector (IV) is generated for each encryption.
29
- *
30
- * @param {string} value - The plain-text string to encrypt.
31
- * @param {string} secretKey - The secret key to use for encryption.
32
- * @returns {Promise<string>} A promise that resolves with a concatenated hexadecimal string of the IV and the ciphertext.
33
- * @throws {Error} If the `secretKey` is null or empty.
16
+ * Encrypts a plain-text string using AES-CBC-256.
17
+ * A unique 16-byte IV is generated per call.
18
+ * Output format: IV_hex (32 chars) + ciphertext_hex.
34
19
  */
35
20
  export declare function encrypt(value: string, secretKey: string): Promise<string>;
36
21
  /**
37
- * Decrypts a hexadecimal string (IV + ciphertext) using AES-CBC with a given secret key.
38
- *
39
- * @param {string} encryptedValue - The concatenated hexadecimal string of the IV and ciphertext.
40
- * @param {string} secretKey - The secret key to use for decryption.
41
- * @returns {Promise<string>} A promise that resolves with the decrypted plain-text string.
42
- * @throws {Error} If the encrypted value is null, empty, or too short, or if the `secretKey` is invalid.
22
+ * Decrypts a hex string (IV_hex + ciphertext_hex) produced by `encrypt()`.
43
23
  */
44
24
  export declare function decrypt(encryptedValue: string, secretKey: string): Promise<string>;
@@ -1,26 +1,37 @@
1
- import { LocationPreference } from "@/types";
2
- import { CookieOptions } from "./ssr";
1
+ import { LocationPreference } from '@/types';
2
+ import { CookieOptions } from './ssr';
3
3
  /**
4
- * Encrypts and stores a key-value pair in either `localStorage`, `sessionStorage`, or cookies.
5
- * Cookies are automatically used in SSR environments and can be explicitly requested.
6
- * Cookies include security options: Secure (HTTPS only), SameSite (CSRF protection), and encryption.
4
+ * Encrypts and stores a value in the requested storage.
7
5
  *
8
- * @param {string} key - The key for the storage item.
9
- * @param {string} value - The string value to encrypt and store.
10
- * @param {string} secretKey - The secret key to use for encryption.
11
- * @param {LocationPreference} location - The storage location: 'local' for `localStorage`, 'session' for `sessionStorage`, 'cookie' for cookies, or 'any' for retrieval.
12
- * @param {CookieOptions} [cookieOptions] - Optional cookie-specific options (only used when location is 'cookie').
13
- * @returns {Promise<void>} A promise that resolves when the item has been stored.
6
+ * | location | where it goes | persistence |
7
+ * |-----------|----------------------------|---------------------|
8
+ * | 'cookie' | document.cookie | expires option |
9
+ * | 'local' | localStorage | until explicitly cleared |
10
+ * | 'any' | localStorage | until explicitly cleared |
11
+ * | 'session' | sessionStorage | until tab closes |
12
+ *
13
+ * Note: 'any' stores in localStorage (same as 'local') for maximum
14
+ * persistence. Previously it stored in sessionStorage — that was a bug.
15
+ *
16
+ * In SSR environments cookies are always used regardless of location
17
+ * (localStorage / sessionStorage do not exist on the server).
14
18
  */
15
19
  export declare function storeEncryptedItem(key: string, value: string, secretKey: string, location: LocationPreference, cookieOptions?: CookieOptions): Promise<void>;
16
20
  /**
17
- * Retrieves and decrypts an item from `localStorage`, `sessionStorage`, or cookies.
18
- * When location is 'any', checks in order: sessionStorage, localStorage, cookies.
19
- * Cookies are automatically checked in SSR environments.
21
+ * Retrieves and decrypts a value from storage.
22
+ *
23
+ * Search order by location:
24
+ *
25
+ * | location | search order |
26
+ * |-----------|-------------------------------------------|
27
+ * | 'cookie' | cookies only |
28
+ * | 'local' | localStorage only |
29
+ * | 'session' | sessionStorage only |
30
+ * | 'any' | sessionStorage → localStorage → cookies |
31
+ *
32
+ * In SSR, cookies are always checked first (localStorage / sessionStorage
33
+ * are not available on the server).
20
34
  *
21
- * @param {string} key - The key of the item to retrieve.
22
- * @param {string} secretKey - The secret key to use for decryption.
23
- * @param {LocationPreference} location - The storage location to search: 'local', 'session', 'cookie', or 'any' (checks session, local, cookie in that order).
24
- * @returns {Promise<string | null>} A promise that resolves with the decrypted value, or `null` if the item is not found or decryption fails.
35
+ * Returns null if the key is not found or decryption fails.
25
36
  */
26
37
  export declare function getDecryptedItem(key: string, secretKey: string, location: LocationPreference): Promise<string | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arex95/vue-core",
3
- "version": "3.3.0",
3
+ "version": "5.1.0",
4
4
  "description": "Opinionated Vue Core",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",