@enfyra/sdk-nuxt 0.3.15 → 0.3.17

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.
Files changed (54) hide show
  1. package/index.ts +3 -0
  2. package/module.d.ts +14 -0
  3. package/{src/module.ts → module.ts} +21 -9
  4. package/package.json +32 -21
  5. package/src/composables/useEnfyraAuth.ts +1 -1
  6. package/src/types/nuxt-imports.d.ts +5 -0
  7. package/dist/auth.d.ts +0 -43
  8. package/dist/auth.d.ts.map +0 -1
  9. package/dist/auth.js +0 -1
  10. package/dist/composables/useEnfyraApi.d.ts +0 -12
  11. package/dist/composables/useEnfyraApi.mjs +0 -345
  12. package/dist/composables/useEnfyraAuth.d.ts +0 -9
  13. package/dist/composables/useEnfyraAuth.mjs +0 -81
  14. package/dist/composables.d.ts +0 -21
  15. package/dist/constants/auth.d.ts +0 -0
  16. package/dist/constants/auth.mjs +0 -3
  17. package/dist/constants/config.d.ts +0 -5
  18. package/dist/constants/config.mjs +0 -1
  19. package/dist/index.d.ts +0 -141
  20. package/dist/index.d.ts.map +0 -1
  21. package/dist/index.js +0 -1
  22. package/dist/module.cjs +0 -77
  23. package/dist/module.d.cts +0 -11
  24. package/dist/module.d.mts +0 -11
  25. package/dist/module.d.ts +0 -11
  26. package/dist/module.json +0 -9
  27. package/dist/module.mjs +0 -74
  28. package/dist/runtime/plugin/config-error.client.d.ts +0 -0
  29. package/dist/runtime/plugin/config-error.client.js +0 -107
  30. package/dist/runtime/plugin/config-error.client.mjs +0 -107
  31. package/dist/runtime/server/api/all.d.ts +0 -0
  32. package/dist/runtime/server/api/all.js +0 -5
  33. package/dist/runtime/server/api/all.mjs +0 -5
  34. package/dist/runtime/server/api/login.post.d.ts +0 -0
  35. package/dist/runtime/server/api/login.post.js +0 -62
  36. package/dist/runtime/server/api/login.post.mjs +0 -62
  37. package/dist/runtime/server/api/logout.post.d.ts +0 -0
  38. package/dist/runtime/server/api/logout.post.js +0 -35
  39. package/dist/runtime/server/api/logout.post.mjs +0 -35
  40. package/dist/runtime/server/middleware/auth.d.ts +0 -0
  41. package/dist/runtime/server/middleware/auth.js +0 -36
  42. package/dist/runtime/server/middleware/auth.mjs +0 -36
  43. package/dist/types.d.mts +0 -7
  44. package/dist/types.d.ts +0 -7
  45. package/dist/utils/config.d.ts +0 -0
  46. package/dist/utils/config.mjs +0 -16
  47. package/dist/utils/http.d.ts +0 -0
  48. package/dist/utils/http.mjs +0 -61
  49. package/dist/utils/server/proxy.d.ts +0 -0
  50. package/dist/utils/server/proxy.mjs +0 -14
  51. package/dist/utils/server/refreshToken.d.ts +0 -0
  52. package/dist/utils/server/refreshToken.mjs +0 -69
  53. package/dist/utils/url.d.ts +0 -0
  54. package/dist/utils/url.mjs +0 -56
@@ -1,107 +0,0 @@
1
- import { defineNuxtPlugin, useRuntimeConfig } from "#imports";
2
- export default defineNuxtPlugin(() => {
3
- const config = useRuntimeConfig().public?.enfyraSDK;
4
- if (config?.configError) {
5
- if (typeof window !== "undefined" && document) {
6
- let showError = function() {
7
- const errorDiv = document.createElement("div");
8
- errorDiv.id = "enfyra-config-error";
9
- errorDiv.style.cssText = `
10
- position: fixed;
11
- top: 0;
12
- left: 0;
13
- right: 0;
14
- bottom: 0;
15
- background: rgba(0, 0, 0, 0.9);
16
- display: flex;
17
- align-items: center;
18
- justify-content: center;
19
- z-index: 999999;
20
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
21
- `;
22
- const content = document.createElement("div");
23
- content.style.cssText = `
24
- background: white;
25
- padding: 40px;
26
- border-radius: 12px;
27
- max-width: 600px;
28
- width: 90%;
29
- box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
30
- `;
31
- content.innerHTML = `
32
- <div style="display: flex; align-items: center; margin-bottom: 20px;">
33
- <svg width="32" height="32" viewBox="0 0 24 24" fill="none" style="margin-right: 12px;">
34
- <circle cx="12" cy="12" r="10" stroke="#ef4444" stroke-width="2"/>
35
- <path d="M12 8v4M12 16h.01" stroke="#ef4444" stroke-width="2" stroke-linecap="round"/>
36
- </svg>
37
- <h1 style="margin: 0; color: #1f2937; font-size: 24px; font-weight: 600;">
38
- Configuration Error
39
- </h1>
40
- </div>
41
-
42
- <div style="color: #4b5563; margin-bottom: 24px; line-height: 1.6;">
43
- <p style="margin: 0 0 16px 0; font-size: 16px;">
44
- <strong>Enfyra SDK</strong> is not configured properly.
45
- </p>
46
- <p style="margin: 0; color: #6b7280; font-size: 14px;">
47
- ${config?.configErrorMessage || "Missing required configuration"}
48
- </p>
49
- </div>
50
-
51
- <div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; margin-bottom: 24px;">
52
- <p style="margin: 0 0 12px 0; color: #6b7280; font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px;">
53
- Add to your nuxt.config.ts:
54
- </p>
55
- <pre style="margin: 0; background: #1f2937; color: #f3f4f6; padding: 16px; border-radius: 6px; overflow-x: auto; font-size: 14px; line-height: 1.4;"><code>export default defineNuxtConfig({
56
- modules: ["@enfyra/sdk-nuxt"],
57
- enfyraSDK: {
58
- <span style="color: #fbbf24;">apiUrl</span>: <span style="color: #34d399;">"https://your-api-url"</span>
59
- }
60
- })</code></pre>
61
- </div>
62
-
63
- <div style="display: flex; gap: 12px;">
64
- <button onclick="location.reload()" style="
65
- background: #3b82f6;
66
- color: white;
67
- border: none;
68
- padding: 10px 20px;
69
- border-radius: 6px;
70
- font-size: 14px;
71
- font-weight: 500;
72
- cursor: pointer;
73
- transition: background 0.2s;
74
- " onmouseover="this.style.background='#2563eb'" onmouseout="this.style.background='#3b82f6'">
75
- Reload Page
76
- </button>
77
- <button onclick="document.getElementById('enfyra-config-error').style.display='none'" style="
78
- background: #f3f4f6;
79
- color: #4b5563;
80
- border: 1px solid #e5e7eb;
81
- padding: 10px 20px;
82
- border-radius: 6px;
83
- font-size: 14px;
84
- font-weight: 500;
85
- cursor: pointer;
86
- transition: all 0.2s;
87
- " onmouseover="this.style.background='#e5e7eb'" onmouseout="this.style.background='#f3f4f6'">
88
- Dismiss
89
- </button>
90
- </div>
91
- `;
92
- errorDiv.appendChild(content);
93
- document.body.appendChild(errorDiv);
94
- console.error(
95
- "%c\u26A0\uFE0F Enfyra SDK Configuration Error",
96
- "color: #ef4444; font-size: 16px; font-weight: bold;",
97
- "\n\n" + (config?.configErrorMessage || "Missing required configuration") + '\n\nPlease add the following to your nuxt.config.ts:\n\nenfyraSDK: {\n apiUrl: "https://your-api-url"\n}'
98
- );
99
- };
100
- if (document.readyState === "loading") {
101
- document.addEventListener("DOMContentLoaded", showError);
102
- } else {
103
- showError();
104
- }
105
- }
106
- }
107
- });
File without changes
@@ -1,5 +0,0 @@
1
- import { defineEventHandler } from "h3";
2
- import { proxyToAPI } from "../../../utils/server/proxy";
3
- export default defineEventHandler(async (event) => {
4
- return proxyToAPI(event);
5
- });
@@ -1,5 +0,0 @@
1
- import { defineEventHandler } from "h3";
2
- import { proxyToAPI } from "../../../utils/server/proxy";
3
- export default defineEventHandler(async (event) => {
4
- return proxyToAPI(event);
5
- });
File without changes
@@ -1,62 +0,0 @@
1
- import {
2
- defineEventHandler,
3
- readBody,
4
- setCookie,
5
- createError,
6
- sendError,
7
- getHeader
8
- } from "h3";
9
- import { useRuntimeConfig } from "#imports";
10
- import { $fetch } from "ofetch";
11
- import {
12
- ACCESS_TOKEN_KEY,
13
- REFRESH_TOKEN_KEY,
14
- EXP_TIME_KEY
15
- } from "../../../constants/auth";
16
- import { normalizeUrl } from "../../../utils/url";
17
- export default defineEventHandler(async (event) => {
18
- const config = useRuntimeConfig();
19
- const apiUrl = config.public.enfyraSDK?.apiUrl;
20
- try {
21
- const body = await readBody(event);
22
- const response = await $fetch(normalizeUrl(apiUrl, "/auth/login"), {
23
- method: "POST",
24
- body,
25
- headers: {
26
- cookie: getHeader(event, "cookie") || ""
27
- }
28
- });
29
- const { accessToken, refreshToken, expTime } = response;
30
- const cookieOptions = {
31
- httpOnly: true,
32
- secure: true,
33
- sameSite: "lax",
34
- path: "/"
35
- };
36
- setCookie(event, ACCESS_TOKEN_KEY, accessToken, cookieOptions);
37
- setCookie(event, REFRESH_TOKEN_KEY, refreshToken, cookieOptions);
38
- setCookie(event, EXP_TIME_KEY, String(expTime), cookieOptions);
39
- return { accessToken };
40
- } catch (err) {
41
- const statusCode = err?.response?.status || err?.statusCode || 401;
42
- const errorData = err?.response?._data || err?.data;
43
- let errorMessage = "Authentication failed";
44
- let errorCode = "AUTHENTICATION_ERROR";
45
- if (errorData?.error) {
46
- errorMessage = errorData.error.message || errorData.message || errorMessage;
47
- errorCode = errorData.error.code || errorCode;
48
- }
49
- return sendError(
50
- event,
51
- createError({
52
- statusCode,
53
- statusMessage: errorMessage,
54
- data: {
55
- code: errorCode,
56
- details: errorData?.error?.details,
57
- correlationId: errorData?.error?.correlationId
58
- }
59
- })
60
- );
61
- }
62
- });
@@ -1,62 +0,0 @@
1
- import {
2
- defineEventHandler,
3
- readBody,
4
- setCookie,
5
- createError,
6
- sendError,
7
- getHeader
8
- } from "h3";
9
- import { useRuntimeConfig } from "#imports";
10
- import { $fetch } from "ofetch";
11
- import {
12
- ACCESS_TOKEN_KEY,
13
- REFRESH_TOKEN_KEY,
14
- EXP_TIME_KEY
15
- } from "../../../constants/auth";
16
- import { normalizeUrl } from "../../../utils/url";
17
- export default defineEventHandler(async (event) => {
18
- const config = useRuntimeConfig();
19
- const apiUrl = config.public.enfyraSDK?.apiUrl;
20
- try {
21
- const body = await readBody(event);
22
- const response = await $fetch(normalizeUrl(apiUrl, "/auth/login"), {
23
- method: "POST",
24
- body,
25
- headers: {
26
- cookie: getHeader(event, "cookie") || ""
27
- }
28
- });
29
- const { accessToken, refreshToken, expTime } = response;
30
- const cookieOptions = {
31
- httpOnly: true,
32
- secure: true,
33
- sameSite: "lax",
34
- path: "/"
35
- };
36
- setCookie(event, ACCESS_TOKEN_KEY, accessToken, cookieOptions);
37
- setCookie(event, REFRESH_TOKEN_KEY, refreshToken, cookieOptions);
38
- setCookie(event, EXP_TIME_KEY, String(expTime), cookieOptions);
39
- return { accessToken };
40
- } catch (err) {
41
- const statusCode = err?.response?.status || err?.statusCode || 401;
42
- const errorData = err?.response?._data || err?.data;
43
- let errorMessage = "Authentication failed";
44
- let errorCode = "AUTHENTICATION_ERROR";
45
- if (errorData?.error) {
46
- errorMessage = errorData.error.message || errorData.message || errorMessage;
47
- errorCode = errorData.error.code || errorCode;
48
- }
49
- return sendError(
50
- event,
51
- createError({
52
- statusCode,
53
- statusMessage: errorMessage,
54
- data: {
55
- code: errorCode,
56
- details: errorData?.error?.details,
57
- correlationId: errorData?.error?.correlationId
58
- }
59
- })
60
- );
61
- }
62
- });
File without changes
@@ -1,35 +0,0 @@
1
- import { defineEventHandler, getHeader, getCookie, deleteCookie } from "h3";
2
- import { useRuntimeConfig } from "#imports";
3
- import { $fetch } from "ofetch";
4
- import {
5
- ACCESS_TOKEN_KEY,
6
- REFRESH_TOKEN_KEY,
7
- EXP_TIME_KEY
8
- } from "../../../constants/auth";
9
- import { normalizeUrl } from "../../../utils/url";
10
- export default defineEventHandler(async (event) => {
11
- const config = useRuntimeConfig();
12
- const apiUrl = config.public?.enfyraSDK?.apiUrl;
13
- const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
14
- try {
15
- const result = await $fetch(normalizeUrl(apiUrl, "/auth/logout"), {
16
- method: "POST",
17
- headers: {
18
- cookie: getHeader(event, "cookie") || "",
19
- authorization: event.context.proxyHeaders?.authorization
20
- },
21
- body: {
22
- refreshToken
23
- }
24
- });
25
- deleteCookie(event, ACCESS_TOKEN_KEY);
26
- deleteCookie(event, REFRESH_TOKEN_KEY);
27
- deleteCookie(event, EXP_TIME_KEY);
28
- return result;
29
- } catch (err) {
30
- deleteCookie(event, ACCESS_TOKEN_KEY);
31
- deleteCookie(event, REFRESH_TOKEN_KEY);
32
- deleteCookie(event, EXP_TIME_KEY);
33
- return { success: false, message: "Logout completed locally" };
34
- }
35
- });
@@ -1,35 +0,0 @@
1
- import { defineEventHandler, getHeader, getCookie, deleteCookie } from "h3";
2
- import { useRuntimeConfig } from "#imports";
3
- import { $fetch } from "ofetch";
4
- import {
5
- ACCESS_TOKEN_KEY,
6
- REFRESH_TOKEN_KEY,
7
- EXP_TIME_KEY
8
- } from "../../../constants/auth";
9
- import { normalizeUrl } from "../../../utils/url";
10
- export default defineEventHandler(async (event) => {
11
- const config = useRuntimeConfig();
12
- const apiUrl = config.public?.enfyraSDK?.apiUrl;
13
- const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
14
- try {
15
- const result = await $fetch(normalizeUrl(apiUrl, "/auth/logout"), {
16
- method: "POST",
17
- headers: {
18
- cookie: getHeader(event, "cookie") || "",
19
- authorization: event.context.proxyHeaders?.authorization
20
- },
21
- body: {
22
- refreshToken
23
- }
24
- });
25
- deleteCookie(event, ACCESS_TOKEN_KEY);
26
- deleteCookie(event, REFRESH_TOKEN_KEY);
27
- deleteCookie(event, EXP_TIME_KEY);
28
- return result;
29
- } catch (err) {
30
- deleteCookie(event, ACCESS_TOKEN_KEY);
31
- deleteCookie(event, REFRESH_TOKEN_KEY);
32
- deleteCookie(event, EXP_TIME_KEY);
33
- return { success: false, message: "Logout completed locally" };
34
- }
35
- });
File without changes
@@ -1,36 +0,0 @@
1
- import { defineEventHandler, getCookie } from "h3";
2
- import { useRuntimeConfig } from "#imports";
3
- import {
4
- validateTokens,
5
- refreshAccessToken
6
- } from "../../../utils/server/refreshToken";
7
- import { REFRESH_TOKEN_KEY } from "../../../constants/auth";
8
- export default defineEventHandler(async (event) => {
9
- if (event.node.req.url === "/api/login" || event.node.req.url === "/api/logout") {
10
- return;
11
- }
12
- const { accessToken, needsRefresh } = validateTokens(event);
13
- let currentAccessToken = accessToken;
14
- if (needsRefresh) {
15
- const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
16
- if (refreshToken) {
17
- try {
18
- const config = useRuntimeConfig();
19
- const apiUrl = config.public?.enfyraSDK?.apiUrl;
20
- if (apiUrl) {
21
- currentAccessToken = await refreshAccessToken(
22
- event,
23
- refreshToken,
24
- apiUrl
25
- );
26
- }
27
- } catch (error) {
28
- currentAccessToken = null;
29
- }
30
- }
31
- }
32
- if (currentAccessToken) {
33
- event.context.proxyHeaders = event.context.proxyHeaders || {};
34
- event.context.proxyHeaders.authorization = `Bearer ${currentAccessToken}`;
35
- }
36
- });
@@ -1,36 +0,0 @@
1
- import { defineEventHandler, getCookie } from "h3";
2
- import { useRuntimeConfig } from "#imports";
3
- import {
4
- validateTokens,
5
- refreshAccessToken
6
- } from "../../../utils/server/refreshToken";
7
- import { REFRESH_TOKEN_KEY } from "../../../constants/auth";
8
- export default defineEventHandler(async (event) => {
9
- if (event.node.req.url === "/api/login" || event.node.req.url === "/api/logout") {
10
- return;
11
- }
12
- const { accessToken, needsRefresh } = validateTokens(event);
13
- let currentAccessToken = accessToken;
14
- if (needsRefresh) {
15
- const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
16
- if (refreshToken) {
17
- try {
18
- const config = useRuntimeConfig();
19
- const apiUrl = config.public?.enfyraSDK?.apiUrl;
20
- if (apiUrl) {
21
- currentAccessToken = await refreshAccessToken(
22
- event,
23
- refreshToken,
24
- apiUrl
25
- );
26
- }
27
- } catch (error) {
28
- currentAccessToken = null;
29
- }
30
- }
31
- }
32
- if (currentAccessToken) {
33
- event.context.proxyHeaders = event.context.proxyHeaders || {};
34
- event.context.proxyHeaders.authorization = `Bearer ${currentAccessToken}`;
35
- }
36
- });
package/dist/types.d.mts DELETED
@@ -1,7 +0,0 @@
1
- import type { NuxtModule } from '@nuxt/schema'
2
-
3
- import type { default as Module } from './module.js'
4
-
5
- export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
-
7
- export { default } from './module.js'
package/dist/types.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { NuxtModule } from '@nuxt/schema'
2
-
3
- import type { default as Module } from './module'
4
-
5
- export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
6
-
7
- export { default } from './module'
File without changes
@@ -1,16 +0,0 @@
1
- import { ref } from "vue";
2
- const config = ref({
3
- apiUrl: "",
4
- defaultHeaders: {}
5
- });
6
- export function useEnfyraConfig() {
7
- const setConfig = (newConfig) => {
8
- config.value = { ...config.value, ...newConfig };
9
- };
10
- const getConfig = () => config.value;
11
- return {
12
- setConfig,
13
- getConfig
14
- };
15
- }
16
- export { config };
File without changes
@@ -1,61 +0,0 @@
1
- export async function $fetch(path, options = {}) {
2
- const {
3
- method = "GET",
4
- body,
5
- headers: optionHeaders = {},
6
- query = {},
7
- baseURL
8
- } = options;
9
- if (!baseURL) {
10
- throw new Error("baseURL is required for $fetch");
11
- }
12
- const url = new URL(
13
- path.startsWith("/") ? path.slice(1) : path,
14
- baseURL.endsWith("/") ? baseURL : `${baseURL}/`
15
- );
16
- Object.entries(query).forEach(([key, value]) => {
17
- if (value !== void 0 && value !== null) {
18
- if (typeof value === "object") {
19
- url.searchParams.append(key, JSON.stringify(value));
20
- } else {
21
- url.searchParams.append(key, String(value));
22
- }
23
- }
24
- });
25
- const headers = {
26
- "Content-Type": "application/json",
27
- ...optionHeaders
28
- };
29
- const fetchOptions = {
30
- method: method.toUpperCase(),
31
- headers
32
- };
33
- if (body && method.toUpperCase() !== "GET") {
34
- if (body instanceof FormData) {
35
- delete headers["Content-Type"];
36
- fetchOptions.body = body;
37
- } else {
38
- fetchOptions.body = JSON.stringify(body);
39
- }
40
- }
41
- try {
42
- const response = await fetch(url.toString(), fetchOptions);
43
- if (!response.ok) {
44
- let errorData;
45
- try {
46
- errorData = await response.json();
47
- } catch {
48
- errorData = { message: response.statusText };
49
- }
50
- throw { response: { data: errorData } };
51
- }
52
- const contentType = response.headers.get("content-type");
53
- if (contentType?.includes("application/json")) {
54
- return await response.json();
55
- } else {
56
- return await response.text();
57
- }
58
- } catch (error) {
59
- throw error;
60
- }
61
- }
File without changes
@@ -1,14 +0,0 @@
1
- import { proxyRequest } from "h3";
2
- import { useRuntimeConfig } from "#imports";
3
- import { ENFYRA_API_PREFIX } from "../../constants/config";
4
- import { normalizeUrl } from "../../utils/url";
5
- export function proxyToAPI(event, customPath) {
6
- const config = useRuntimeConfig();
7
- const apiPrefix = config.public?.enfyraSDK?.apiPrefix || ENFYRA_API_PREFIX;
8
- const rawPath = customPath || event.path.replace(new RegExp(`^${apiPrefix}`), "");
9
- const targetUrl = normalizeUrl(config.public?.enfyraSDK?.apiUrl, rawPath);
10
- const headers = event.context.proxyHeaders || {};
11
- return proxyRequest(event, targetUrl, {
12
- headers
13
- });
14
- }
File without changes
@@ -1,69 +0,0 @@
1
- import { setCookie, getCookie, deleteCookie } from "h3";
2
- import { $fetch } from "ofetch";
3
- import {
4
- ACCESS_TOKEN_KEY,
5
- REFRESH_TOKEN_KEY,
6
- EXP_TIME_KEY
7
- } from "../../constants/auth";
8
- import { normalizeUrl } from "../../utils/url";
9
- export function decodeJWT(token) {
10
- try {
11
- const parts = token.split(".");
12
- if (parts.length !== 3) {
13
- return null;
14
- }
15
- const payload = parts[1];
16
- const decodedPayload = Buffer.from(payload, "base64url").toString("utf-8");
17
- return JSON.parse(decodedPayload);
18
- } catch (error) {
19
- console.warn("Failed to decode JWT:", error);
20
- return null;
21
- }
22
- }
23
- export function isAccessTokenExpired(accessToken) {
24
- const decoded = decodeJWT(accessToken);
25
- if (!decoded || !decoded.exp) {
26
- return true;
27
- }
28
- const expirationTime = decoded.exp * 1e3;
29
- return Date.now() >= expirationTime;
30
- }
31
- export function validateTokens(event) {
32
- const accessToken = getCookie(event, ACCESS_TOKEN_KEY);
33
- const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
34
- if (accessToken && !isAccessTokenExpired(accessToken)) {
35
- return { accessToken, needsRefresh: false };
36
- } else if (refreshToken && (!accessToken || isAccessTokenExpired(accessToken))) {
37
- return { accessToken: null, needsRefresh: true };
38
- }
39
- return { accessToken: null, needsRefresh: false };
40
- }
41
- export async function refreshAccessToken(event, refreshToken, apiUrl) {
42
- try {
43
- const response = await $fetch(normalizeUrl(apiUrl, "/auth/refresh-token"), {
44
- method: "POST",
45
- body: { refreshToken }
46
- });
47
- const {
48
- accessToken: newAccessToken,
49
- refreshToken: newRefreshToken,
50
- expTime: newExpTime
51
- } = response;
52
- const cookieOptions = {
53
- httpOnly: true,
54
- secure: true,
55
- sameSite: "lax",
56
- path: "/"
57
- };
58
- setCookie(event, ACCESS_TOKEN_KEY, newAccessToken, cookieOptions);
59
- setCookie(event, REFRESH_TOKEN_KEY, newRefreshToken, cookieOptions);
60
- setCookie(event, EXP_TIME_KEY, String(newExpTime), cookieOptions);
61
- return newAccessToken;
62
- } catch (error) {
63
- console.warn("Token refresh failed:", error);
64
- deleteCookie(event, ACCESS_TOKEN_KEY);
65
- deleteCookie(event, REFRESH_TOKEN_KEY);
66
- deleteCookie(event, EXP_TIME_KEY);
67
- throw error;
68
- }
69
- }
File without changes
@@ -1,56 +0,0 @@
1
- export function normalizeUrl(...segments) {
2
- const validSegments = segments.filter((s) => Boolean(s));
3
- if (validSegments.length === 0) return "";
4
- let result = validSegments[0].replace(/\/+$/, "");
5
- for (let i = 1; i < validSegments.length; i++) {
6
- const segment = validSegments[i].replace(/^\/+/, "").replace(/\/+$/, "").replace(/\/+/g, "/");
7
- if (segment) {
8
- result += "/" + segment;
9
- }
10
- }
11
- return result;
12
- }
13
- export function joinUrlPath(...paths) {
14
- const validPaths = paths.filter((p) => Boolean(p));
15
- if (validPaths.length === 0) return "";
16
- return validPaths.map((path) => path.replace(/^\/+/, "").replace(/\/+$/, "")).filter(Boolean).join("/");
17
- }
18
- export function getAppUrl() {
19
- if (process.client && typeof window !== "undefined") {
20
- return window.location.origin;
21
- }
22
- if (process.server) {
23
- try {
24
- let useRequestHeaders;
25
- let useRequestURL;
26
- try {
27
- const imports = eval('require("#imports")');
28
- useRequestHeaders = imports.useRequestHeaders;
29
- useRequestURL = imports.useRequestURL;
30
- } catch (e) {
31
- return "";
32
- }
33
- try {
34
- const url = useRequestURL();
35
- if (url) {
36
- return `${url.protocol}//${url.host}`;
37
- }
38
- } catch (e) {
39
- }
40
- const headers = useRequestHeaders();
41
- const forwarded = headers["x-forwarded-host"] || headers["x-forwarded-server"];
42
- const protocol = headers["x-forwarded-proto"] || "https";
43
- if (forwarded) {
44
- return `${protocol}://${forwarded}`;
45
- }
46
- const host = headers.host;
47
- if (host) {
48
- const isHttps = protocol === "https" || headers["x-forwarded-ssl"] === "on";
49
- return `${isHttps ? "https" : "http"}://${host}`;
50
- }
51
- } catch (e) {
52
- console.warn("[Enfyra SDK] Could not auto-detect app URL on server:", e);
53
- }
54
- }
55
- return "";
56
- }