@enfyra/sdk-nuxt 0.3.17 → 0.3.18
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.d.ts +10 -0
- package/dist/runtime/plugin/config-error.client.d.ts +0 -0
- package/dist/runtime/plugin/config-error.client.js +107 -0
- package/dist/runtime/plugin/config-error.client.mjs +107 -0
- package/dist/runtime/server/api/all.d.ts +0 -0
- package/dist/runtime/server/api/all.js +5 -0
- package/dist/runtime/server/api/all.mjs +5 -0
- package/dist/runtime/server/api/login.post.d.ts +0 -0
- package/dist/runtime/server/api/login.post.js +62 -0
- package/dist/runtime/server/api/login.post.mjs +62 -0
- package/dist/runtime/server/api/logout.post.d.ts +0 -0
- package/dist/runtime/server/api/logout.post.js +35 -0
- package/dist/runtime/server/api/logout.post.mjs +35 -0
- package/dist/runtime/server/middleware/auth.d.ts +0 -0
- package/dist/runtime/server/middleware/auth.js +36 -0
- package/dist/runtime/server/middleware/auth.mjs +36 -0
- package/module.ts +6 -6
- package/package.json +7 -4
- package/src/utils/url.ts +4 -11
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
interface ModuleOptions {
|
|
4
|
+
apiUrl: string;
|
|
5
|
+
apiPrefix?: string;
|
|
6
|
+
}
|
|
7
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
8
|
+
|
|
9
|
+
export { _default as default };
|
|
10
|
+
export type { ModuleOptions };
|
|
File without changes
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
|
File without changes
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
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/module.ts
CHANGED
|
@@ -65,7 +65,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
65
65
|
|
|
66
66
|
if (!normalizedOptions.apiUrl) {
|
|
67
67
|
addPlugin({
|
|
68
|
-
src: resolve("./
|
|
68
|
+
src: resolve("./dist/runtime/plugin/config-error.client.mjs"),
|
|
69
69
|
mode: 'client'
|
|
70
70
|
});
|
|
71
71
|
}
|
|
@@ -79,31 +79,31 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
79
79
|
})
|
|
80
80
|
|
|
81
81
|
addServerHandler({
|
|
82
|
-
handler: '
|
|
82
|
+
handler: resolve('./dist/runtime/server/middleware/auth.mjs'),
|
|
83
83
|
middleware: true,
|
|
84
84
|
});
|
|
85
85
|
|
|
86
86
|
addServerHandler({
|
|
87
87
|
route: `${apiPrefix}/login`,
|
|
88
|
-
handler: '
|
|
88
|
+
handler: resolve('./dist/runtime/server/api/login.post.mjs'),
|
|
89
89
|
method: "post",
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
addServerHandler({
|
|
93
93
|
route: `${apiPrefix}/logout`,
|
|
94
|
-
handler: '
|
|
94
|
+
handler: resolve('./dist/runtime/server/api/logout.post.mjs'),
|
|
95
95
|
method: "post",
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
|
|
99
99
|
addServerHandler({
|
|
100
100
|
route: "/assets/**",
|
|
101
|
-
handler: '
|
|
101
|
+
handler: resolve('./dist/runtime/server/api/all.mjs'),
|
|
102
102
|
});
|
|
103
103
|
|
|
104
104
|
addServerHandler({
|
|
105
105
|
route: `${apiPrefix}/**`,
|
|
106
|
-
handler: '
|
|
106
|
+
handler: resolve('./dist/runtime/server/api/all.mjs'),
|
|
107
107
|
});
|
|
108
108
|
},
|
|
109
109
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enfyra/sdk-nuxt",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.18",
|
|
4
4
|
"description": "Nuxt SDK for Enfyra CMS",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -42,7 +42,8 @@
|
|
|
42
42
|
"module.ts",
|
|
43
43
|
"module.d.ts",
|
|
44
44
|
"index.ts",
|
|
45
|
-
"src/**/*"
|
|
45
|
+
"src/**/*",
|
|
46
|
+
"dist/runtime/**/*"
|
|
46
47
|
],
|
|
47
48
|
"keywords": [
|
|
48
49
|
"nuxt",
|
|
@@ -56,7 +57,8 @@
|
|
|
56
57
|
"access": "public"
|
|
57
58
|
},
|
|
58
59
|
"scripts": {
|
|
59
|
-
"build": "npx tsc --noEmit --skipLibCheck"
|
|
60
|
+
"build": "unbuild && npx tsc --noEmit --skipLibCheck",
|
|
61
|
+
"prepublishOnly": "unbuild"
|
|
60
62
|
},
|
|
61
63
|
"peerDependencies": {
|
|
62
64
|
"@nuxt/kit": "^3.18.1",
|
|
@@ -69,6 +71,7 @@
|
|
|
69
71
|
"devDependencies": {
|
|
70
72
|
"@types/node": "^24.10.1",
|
|
71
73
|
"nuxt": "^3.18.1",
|
|
72
|
-
"typescript": "^5.0.0"
|
|
74
|
+
"typescript": "^5.0.0",
|
|
75
|
+
"unbuild": "^2.0.0"
|
|
73
76
|
}
|
|
74
77
|
}
|
package/src/utils/url.ts
CHANGED
|
@@ -37,6 +37,9 @@ export function joinUrlPath(...paths: (string | undefined | null)[]): string {
|
|
|
37
37
|
.join('/');
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
// Import from #imports at top level
|
|
41
|
+
import { useRequestHeaders, useRequestURL } from '#imports';
|
|
42
|
+
|
|
40
43
|
export function getAppUrl(): string {
|
|
41
44
|
if (process.client && typeof window !== 'undefined') {
|
|
42
45
|
return window.location.origin;
|
|
@@ -44,23 +47,13 @@ export function getAppUrl(): string {
|
|
|
44
47
|
|
|
45
48
|
if (process.server) {
|
|
46
49
|
try {
|
|
47
|
-
let useRequestHeaders: any;
|
|
48
|
-
let useRequestURL: any;
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const imports = eval('require("#imports")');
|
|
52
|
-
useRequestHeaders = imports.useRequestHeaders;
|
|
53
|
-
useRequestURL = imports.useRequestURL;
|
|
54
|
-
} catch (e) {
|
|
55
|
-
return '';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
50
|
try {
|
|
59
51
|
const url = useRequestURL();
|
|
60
52
|
if (url) {
|
|
61
53
|
return `${url.protocol}//${url.host}`;
|
|
62
54
|
}
|
|
63
55
|
} catch (e) {
|
|
56
|
+
// useRequestURL might not be available in all contexts
|
|
64
57
|
}
|
|
65
58
|
|
|
66
59
|
const headers = useRequestHeaders();
|