@enfyra/sdk-nuxt 0.3.7 → 0.3.9
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/module.json
CHANGED
package/dist/utils/config.mjs
CHANGED
|
@@ -5,7 +5,11 @@ const config = ref({
|
|
|
5
5
|
});
|
|
6
6
|
export function useEnfyraConfig() {
|
|
7
7
|
const setConfig = (newConfig) => {
|
|
8
|
-
|
|
8
|
+
const normalizedConfig = { ...newConfig };
|
|
9
|
+
if (typeof normalizedConfig.apiUrl === "string") {
|
|
10
|
+
normalizedConfig.apiUrl = normalizedConfig.apiUrl.replace(/\/+$/, "");
|
|
11
|
+
}
|
|
12
|
+
config.value = { ...config.value, ...normalizedConfig };
|
|
9
13
|
};
|
|
10
14
|
const getConfig = () => config.value;
|
|
11
15
|
return {
|
|
@@ -5,14 +5,34 @@ import {
|
|
|
5
5
|
REFRESH_TOKEN_KEY,
|
|
6
6
|
EXP_TIME_KEY
|
|
7
7
|
} from "../../constants/auth";
|
|
8
|
+
export function decodeJWT(token) {
|
|
9
|
+
try {
|
|
10
|
+
const parts = token.split(".");
|
|
11
|
+
if (parts.length !== 3) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const payload = parts[1];
|
|
15
|
+
const decodedPayload = Buffer.from(payload, "base64url").toString("utf-8");
|
|
16
|
+
return JSON.parse(decodedPayload);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
console.warn("Failed to decode JWT:", error);
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export function isAccessTokenExpired(accessToken) {
|
|
23
|
+
const decoded = decodeJWT(accessToken);
|
|
24
|
+
if (!decoded || !decoded.exp) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
const expirationTime = decoded.exp * 1e3;
|
|
28
|
+
return Date.now() >= expirationTime;
|
|
29
|
+
}
|
|
8
30
|
export function validateTokens(event) {
|
|
9
31
|
const accessToken = getCookie(event, ACCESS_TOKEN_KEY);
|
|
10
32
|
const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
|
|
11
|
-
|
|
12
|
-
const isTokenExpired = expTime && Date.now() > parseInt(expTime);
|
|
13
|
-
if (accessToken && !isTokenExpired) {
|
|
33
|
+
if (accessToken && !isAccessTokenExpired(accessToken)) {
|
|
14
34
|
return { accessToken, needsRefresh: false };
|
|
15
|
-
} else if (refreshToken && (
|
|
35
|
+
} else if (refreshToken && (!accessToken || isAccessTokenExpired(accessToken))) {
|
|
16
36
|
return { accessToken: null, needsRefresh: true };
|
|
17
37
|
}
|
|
18
38
|
return { accessToken: null, needsRefresh: false };
|
package/package.json
CHANGED
package/src/utils/config.ts
CHANGED
|
@@ -8,7 +8,13 @@ const config = ref<EnfyraConfig>({
|
|
|
8
8
|
|
|
9
9
|
export function useEnfyraConfig() {
|
|
10
10
|
const setConfig = (newConfig: Partial<EnfyraConfig>) => {
|
|
11
|
-
|
|
11
|
+
const normalizedConfig = { ...newConfig };
|
|
12
|
+
|
|
13
|
+
if (typeof normalizedConfig.apiUrl === 'string') {
|
|
14
|
+
normalizedConfig.apiUrl = normalizedConfig.apiUrl.replace(/\/+$/, '');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
config.value = { ...config.value, ...normalizedConfig };
|
|
12
18
|
};
|
|
13
19
|
|
|
14
20
|
const getConfig = () => config.value;
|
|
@@ -11,16 +11,41 @@ interface TokenValidationResult {
|
|
|
11
11
|
needsRefresh: boolean;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
export function decodeJWT(token: string): any {
|
|
15
|
+
try {
|
|
16
|
+
const parts = token.split(".");
|
|
17
|
+
if (parts.length !== 3) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Decode the payload (second part)
|
|
22
|
+
const payload = parts[1];
|
|
23
|
+
const decodedPayload = Buffer.from(payload, "base64url").toString("utf-8");
|
|
24
|
+
return JSON.parse(decodedPayload);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.warn("Failed to decode JWT:", error);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isAccessTokenExpired(accessToken: string): boolean {
|
|
32
|
+
const decoded = decodeJWT(accessToken);
|
|
33
|
+
if (!decoded || !decoded.exp) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// JWT exp is in seconds, Date.now() is in milliseconds
|
|
38
|
+
const expirationTime = decoded.exp * 1000;
|
|
39
|
+
return Date.now() >= expirationTime;
|
|
40
|
+
}
|
|
41
|
+
|
|
14
42
|
export function validateTokens(event: H3Event): TokenValidationResult {
|
|
15
43
|
const accessToken = getCookie(event, ACCESS_TOKEN_KEY);
|
|
16
44
|
const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
|
|
17
|
-
const expTime = getCookie(event, EXP_TIME_KEY);
|
|
18
|
-
|
|
19
|
-
const isTokenExpired = expTime && Date.now() > parseInt(expTime);
|
|
20
45
|
|
|
21
|
-
if (accessToken && !
|
|
46
|
+
if (accessToken && !isAccessTokenExpired(accessToken)) {
|
|
22
47
|
return { accessToken, needsRefresh: false };
|
|
23
|
-
} else if (refreshToken && (
|
|
48
|
+
} else if (refreshToken && (!accessToken || isAccessTokenExpired(accessToken))) {
|
|
24
49
|
return { accessToken: null, needsRefresh: true };
|
|
25
50
|
}
|
|
26
51
|
|