@arex95/vue-core 1.1.30 → 1.1.33

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,4 +1,4 @@
1
- import { AuthParams, AuthResponse, AuthTokenPaths, SessionPreference } from "@/types";
1
+ import { AuthParams, AuthResponse, AuthTokenPaths, LocationPreference } from "@/types";
2
2
  /**
3
3
  * @typedef {object} AuthHook
4
4
  * @property {function(): Promise<number | null>} getTokenExpiry - Retrieves the expiration time of the current token in milliseconds.
@@ -18,6 +18,6 @@ import { AuthParams, AuthResponse, AuthTokenPaths, SessionPreference } from "@/t
18
18
  */
19
19
  export declare function useAuth(secretKey?: string): {
20
20
  logout: (params?: AuthParams) => Promise<void>;
21
- login: (params: AuthParams, persistence: SessionPreference, tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>;
21
+ login: (params: AuthParams, persistence: LocationPreference, tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>;
22
22
  refresh: (tokenPaths?: AuthTokenPaths) => Promise<AuthResponse>;
23
23
  };
@@ -1,4 +1,4 @@
1
- import { SessionPreference, SessionConfigObject, SessionConfig } from "@/types/SessionConfig";
1
+ import { LocationPreference, SessionConfigObject, SessionConfig } from "@/types/SessionConfig";
2
2
  /**
3
3
  * Configures the session identifier and/or data persistence preference
4
4
  * for the active browser session.
@@ -24,7 +24,7 @@ export declare function getSessionId(): Promise<string>;
24
24
  *
25
25
  * @returns {Promise<SessionPreference>} A promise that resolves with the configured persistence preference ('local' or 'session').
26
26
  */
27
- export declare function getSessionPersistence(): Promise<SessionPreference>;
27
+ export declare function getSessionPersistence(): Promise<LocationPreference>;
28
28
  /**
29
29
  * Retrieves the complete session configuration.
30
30
  * Always attempts to load the configuration from storage. If it fails, it uses the internal state.
package/dist/index.mjs CHANGED
@@ -623,7 +623,7 @@ class AxiosService {
623
623
  timeout: 300000,
624
624
  headers: {
625
625
  'Accept': 'application/json',
626
- 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
626
+ 'Cache-Control': 'no-cache',
627
627
  'Pragma': 'no-cache',
628
628
  'Expires': '0',
629
629
  ...headers,
@@ -866,14 +866,14 @@ async function decrypt(encryptedValue, secretKey) {
866
866
  * @param key The key under which to store the value.
867
867
  * @param value The value to encrypt and store.
868
868
  * @param secretKey The secret key for encryption.
869
- * @param persistent If true, uses localStorage; otherwise, uses sessionStorage.
869
+ * @param location Determines where the item is stored: 'local' for localStorage, 'session' for sessionStorage.
870
870
  * @returns A promise that resolves when the item is stored. Throws an error if it fails.
871
871
  */
872
- async function storeEncryptedItem(key, value, secretKey, persistent) {
872
+ async function storeEncryptedItem(key, value, secretKey, location) {
873
873
  if (typeof window === "undefined") {
874
874
  throw new Error("Cannot access storage: window is not defined.");
875
875
  }
876
- const storage = persistent ? window.localStorage : window.sessionStorage;
876
+ const storage = location === "local" ? window.localStorage : window.sessionStorage;
877
877
  const encryptedValue = await encrypt(value, secretKey);
878
878
  storage.setItem(key, encryptedValue);
879
879
  }
@@ -882,15 +882,20 @@ async function storeEncryptedItem(key, value, secretKey, persistent) {
882
882
  * Assumes the `window` environment is available.
883
883
  * @param key The key of the item to retrieve.
884
884
  * @param secretKey The secret key for decryption.
885
- * @param persistent If true, searches localStorage; otherwise, sessionStorage.
885
+ * @param location Specifies where to search for the item: 'local' for localStorage, 'session' for sessionStorage, or 'any' to check both (session first).
886
886
  * @returns A promise that resolves with the decrypted value or null if not found or decryption fails.
887
887
  */
888
- async function getDecryptedItem(key, secretKey, persistent) {
888
+ async function getDecryptedItem(key, secretKey, location) {
889
889
  if (typeof window === "undefined") {
890
890
  return null;
891
891
  }
892
- const storage = persistent ? window.localStorage : window.sessionStorage;
893
- const encryptedData = storage.getItem(key);
892
+ let encryptedData = null;
893
+ if (location === "session" || location === "any") {
894
+ encryptedData = window.sessionStorage.getItem(key);
895
+ }
896
+ if (!encryptedData && (location === "local" || location === "any")) {
897
+ encryptedData = window.localStorage.getItem(key);
898
+ }
894
899
  if (!encryptedData) {
895
900
  return null;
896
901
  }
@@ -957,9 +962,9 @@ function updateSessionConfig() {
957
962
  * @returns {Promise<void>} A promise that resolves when the session configuration has been saved.
958
963
  */
959
964
  async function saveSessionConfig() {
960
- const isPersistent = internalSessionState.persistencePreference === "local";
965
+ const location = internalSessionState.persistencePreference;
961
966
  try {
962
- await storeEncryptedItem(SESSION_KEY, JSON.stringify(_sessionConfig), getAppKey(), isPersistent);
967
+ await storeEncryptedItem(SESSION_KEY, JSON.stringify(_sessionConfig), getAppKey(), location);
963
968
  }
964
969
  catch (error) {
965
970
  console.error("Error saving session configuration to storage:", error);
@@ -973,8 +978,9 @@ async function saveSessionConfig() {
973
978
  * @returns {Promise<void>} A promise that resolves when the state has been loaded and updated.
974
979
  */
975
980
  async function loadSessionConfig() {
981
+ const location = internalSessionState.persistencePreference;
976
982
  try {
977
- const storedConfig = await getDecryptedItem(SESSION_KEY, getAppKey(), true);
983
+ const storedConfig = await getDecryptedItem(SESSION_KEY, getAppKey(), location);
978
984
  if (storedConfig) {
979
985
  const parsedConfig = JSON.parse(storedConfig);
980
986
  internalSessionState.sessionId = parsedConfig.SESSION_ID;
@@ -2702,16 +2708,21 @@ function isValidHexNumber(hex) {
2702
2708
 
2703
2709
  /**
2704
2710
  * Clears all stored authentication data (access and refresh tokens)
2705
- * from either sessionStorage or localStorage based on the provided preference.
2711
+ * from either sessionStorage, localStorage, or both based on the provided location preference.
2706
2712
  *
2707
- * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
2713
+ * @param {LocationPreference} location - The storage preference ('local' for localStorage, 'session' for sessionStorage, 'any' for both).
2708
2714
  * @returns {Promise<void>} A promise that resolves when all relevant storage items are removed.
2709
2715
  */
2710
- const cleanCredentials = async (preference) => {
2716
+ const cleanCredentials = async (location) => {
2711
2717
  const tokensConfig = getTokenConfig();
2712
2718
  Object.keys(tokensConfig).forEach((key) => {
2713
- const storage = preference === "local" ? localStorage : sessionStorage;
2714
- storage.removeItem(tokensConfig[key]);
2719
+ const itemKey = tokensConfig[key];
2720
+ if (location === "local" || location === "any") {
2721
+ localStorage.removeItem(itemKey);
2722
+ }
2723
+ if (location === "session" || location === "any") {
2724
+ sessionStorage.removeItem(itemKey);
2725
+ }
2715
2726
  });
2716
2727
  };
2717
2728
  /**
@@ -2722,9 +2733,9 @@ const cleanCredentials = async (preference) => {
2722
2733
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
2723
2734
  * @returns {Promise<string | null>} A promise that resolves with the decrypted access token, or null if not found.
2724
2735
  */
2725
- const getAuthToken = async (secretKey, preference) => {
2736
+ const getAuthToken = async (secretKey, location) => {
2726
2737
  const tokensConfig = getTokenConfig();
2727
- return await getDecryptedItem(tokensConfig.ACCESS_TOKEN, secretKey, preference === "local");
2738
+ return await getDecryptedItem(tokensConfig.ACCESS_TOKEN, secretKey, location);
2728
2739
  };
2729
2740
  /**
2730
2741
  * Retrieves the authentication refresh token from storage, decrypting it
@@ -2734,9 +2745,9 @@ const getAuthToken = async (secretKey, preference) => {
2734
2745
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
2735
2746
  * @returns {Promise<string | null>} A promise that resolves with the decrypted refresh token, or null if not found.
2736
2747
  */
2737
- const getAuthRefreshToken = async (secretKey, preference) => {
2748
+ const getAuthRefreshToken = async (secretKey, location) => {
2738
2749
  const tokensConfig = getTokenConfig();
2739
- return await getDecryptedItem(tokensConfig.REFRESH_TOKEN, secretKey, preference === "local");
2750
+ return await getDecryptedItem(tokensConfig.REFRESH_TOKEN, secretKey, location);
2740
2751
  };
2741
2752
  /**
2742
2753
  * Stores the authentication token (access token) in storage after encrypting it,
@@ -2747,9 +2758,9 @@ const getAuthRefreshToken = async (secretKey, preference) => {
2747
2758
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
2748
2759
  * @returns {Promise<void>} A promise that resolves when the token is successfully stored.
2749
2760
  */
2750
- const storeAuthToken = async (token, secretKey, preference) => {
2761
+ const storeAuthToken = async (token, secretKey, location) => {
2751
2762
  const tokensConfig = getTokenConfig();
2752
- await storeEncryptedItem(tokensConfig.ACCESS_TOKEN, token, secretKey, preference === "local");
2763
+ await storeEncryptedItem(tokensConfig.ACCESS_TOKEN, token, secretKey, location);
2753
2764
  };
2754
2765
  /**
2755
2766
  * Stores the authentication refresh token in storage after encrypting it,
@@ -2760,9 +2771,9 @@ const storeAuthToken = async (token, secretKey, preference) => {
2760
2771
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
2761
2772
  * @returns {Promise<void>} A promise that resolves when the token is successfully stored.
2762
2773
  */
2763
- const storeAuthRefreshToken = async (token, secretKey, preference) => {
2774
+ const storeAuthRefreshToken = async (token, secretKey, location) => {
2764
2775
  const tokensConfig = getTokenConfig();
2765
- await storeEncryptedItem(tokensConfig.REFRESH_TOKEN, token, secretKey, preference === "local");
2776
+ await storeEncryptedItem(tokensConfig.REFRESH_TOKEN, token, secretKey, location);
2766
2777
  };
2767
2778
  /**
2768
2779
  * Verifies the validity and expiration of the current authentication token.
@@ -2773,7 +2784,7 @@ const storeAuthRefreshToken = async (token, secretKey, preference) => {
2773
2784
  * "TOKEN_INVALID" if the token format is invalid.
2774
2785
  */
2775
2786
  const verifyAuth = async () => {
2776
- const sessionPersistence = await getSessionPersistence();
2787
+ const sessionPersistence = 'any';
2777
2788
  const handleAuthError = async (message, shouldClean = true) => {
2778
2789
  handleError(message, false);
2779
2790
  if (shouldClean) {
@@ -1,13 +1,13 @@
1
- export type SessionPreference = "local" | "session";
1
+ export type LocationPreference = "local" | "session" | "any";
2
2
  export type SessionConfig = {
3
3
  SESSION_ID: string;
4
- PERSISTENCE: SessionPreference;
4
+ PERSISTENCE: LocationPreference;
5
5
  };
6
6
  export interface InternalSessionState {
7
7
  sessionId: string;
8
- persistencePreference: SessionPreference;
8
+ persistencePreference: LocationPreference;
9
9
  }
10
10
  export interface SessionConfigObject {
11
11
  sessionId?: string;
12
- persistencePreference?: SessionPreference;
12
+ persistencePreference?: LocationPreference;
13
13
  }
@@ -1,12 +1,12 @@
1
- import { SessionPreference } from "@/types/SessionConfig";
1
+ import { LocationPreference } from "@/types/SessionConfig";
2
2
  /**
3
3
  * Clears all stored authentication data (access and refresh tokens)
4
- * from either sessionStorage or localStorage based on the provided preference.
4
+ * from either sessionStorage, localStorage, or both based on the provided location preference.
5
5
  *
6
- * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
6
+ * @param {LocationPreference} location - The storage preference ('local' for localStorage, 'session' for sessionStorage, 'any' for both).
7
7
  * @returns {Promise<void>} A promise that resolves when all relevant storage items are removed.
8
8
  */
9
- export declare const cleanCredentials: (preference: SessionPreference) => Promise<void>;
9
+ export declare const cleanCredentials: (location: LocationPreference) => Promise<void>;
10
10
  /**
11
11
  * Retrieves the authentication token (access token) from storage, decrypting it
12
12
  * using the provided secret key and based on the specified session preference.
@@ -15,7 +15,7 @@ export declare const cleanCredentials: (preference: SessionPreference) => Promis
15
15
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
16
16
  * @returns {Promise<string | null>} A promise that resolves with the decrypted access token, or null if not found.
17
17
  */
18
- export declare const getAuthToken: (secretKey: string, preference: SessionPreference) => Promise<string | null>;
18
+ export declare const getAuthToken: (secretKey: string, location: LocationPreference) => Promise<string | null>;
19
19
  /**
20
20
  * Retrieves the authentication refresh token from storage, decrypting it
21
21
  * using the provided secret key and based on the specified session preference.
@@ -24,7 +24,7 @@ export declare const getAuthToken: (secretKey: string, preference: SessionPrefer
24
24
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
25
25
  * @returns {Promise<string | null>} A promise that resolves with the decrypted refresh token, or null if not found.
26
26
  */
27
- export declare const getAuthRefreshToken: (secretKey: string, preference: SessionPreference) => Promise<string | null>;
27
+ export declare const getAuthRefreshToken: (secretKey: string, location: LocationPreference) => Promise<string | null>;
28
28
  /**
29
29
  * Stores the authentication token (access token) in storage after encrypting it,
30
30
  * based on the specified session preference.
@@ -34,7 +34,7 @@ export declare const getAuthRefreshToken: (secretKey: string, preference: Sessio
34
34
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
35
35
  * @returns {Promise<void>} A promise that resolves when the token is successfully stored.
36
36
  */
37
- export declare const storeAuthToken: (token: string, secretKey: string, preference: SessionPreference) => Promise<void>;
37
+ export declare const storeAuthToken: (token: string, secretKey: string, location: LocationPreference) => Promise<void>;
38
38
  /**
39
39
  * Stores the authentication refresh token in storage after encrypting it,
40
40
  * based on the specified session preference.
@@ -44,7 +44,7 @@ export declare const storeAuthToken: (token: string, secretKey: string, preferen
44
44
  * @param {SessionPreference} preference - The storage preference ('local' for localStorage, 'session' for sessionStorage).
45
45
  * @returns {Promise<void>} A promise that resolves when the token is successfully stored.
46
46
  */
47
- export declare const storeAuthRefreshToken: (token: string, secretKey: string, preference: SessionPreference) => Promise<void>;
47
+ export declare const storeAuthRefreshToken: (token: string, secretKey: string, location: LocationPreference) => Promise<void>;
48
48
  /**
49
49
  * Verifies the validity and expiration of the current authentication token.
50
50
  * If the token is missing, invalid, or expired, appropriate errors are thrown and credentials are cleaned.
@@ -1,19 +1,20 @@
1
+ import { LocationPreference } from "@/types";
1
2
  /**
2
3
  * Encrypts and stores an item in local or session storage.
3
4
  * Assumes the `window` environment is available.
4
5
  * @param key The key under which to store the value.
5
6
  * @param value The value to encrypt and store.
6
7
  * @param secretKey The secret key for encryption.
7
- * @param persistent If true, uses localStorage; otherwise, uses sessionStorage.
8
+ * @param location Determines where the item is stored: 'local' for localStorage, 'session' for sessionStorage.
8
9
  * @returns A promise that resolves when the item is stored. Throws an error if it fails.
9
10
  */
10
- export declare function storeEncryptedItem(key: string, value: string, secretKey: string, persistent: boolean): Promise<void>;
11
+ export declare function storeEncryptedItem(key: string, value: string, secretKey: string, location: LocationPreference): Promise<void>;
11
12
  /**
12
13
  * Retrieves and decrypts a value from local or session storage.
13
14
  * Assumes the `window` environment is available.
14
15
  * @param key The key of the item to retrieve.
15
16
  * @param secretKey The secret key for decryption.
16
- * @param persistent If true, searches localStorage; otherwise, sessionStorage.
17
+ * @param location Specifies where to search for the item: 'local' for localStorage, 'session' for sessionStorage, or 'any' to check both (session first).
17
18
  * @returns A promise that resolves with the decrypted value or null if not found or decryption fails.
18
19
  */
19
- export declare function getDecryptedItem(key: string, secretKey: string, persistent: boolean): Promise<string | null>;
20
+ 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": "1.1.30",
3
+ "version": "1.1.33",
4
4
  "description": "Opinionated Vue Core",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",