@chemmangat/msal-next 3.1.6 → 3.1.8
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/CHANGELOG.md +33 -36
- package/README.md +7 -65
- package/dist/index.d.mts +205 -59
- package/dist/index.d.ts +205 -59
- package/dist/index.js +2 -1726
- package/dist/index.mjs +2 -1671
- package/dist/server.js +1 -115
- package/dist/server.mjs +1 -87
- package/package.json +3 -2
package/dist/server.js
CHANGED
|
@@ -1,115 +1 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/server.ts
|
|
21
|
-
var server_exports = {};
|
|
22
|
-
__export(server_exports, {
|
|
23
|
-
getServerSession: () => getServerSession,
|
|
24
|
-
setServerSessionCookie: () => setServerSessionCookie
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(server_exports);
|
|
27
|
-
|
|
28
|
-
// src/utils/getServerSession.ts
|
|
29
|
-
var import_headers = require("next/headers");
|
|
30
|
-
|
|
31
|
-
// src/utils/validation.ts
|
|
32
|
-
function safeJsonParse(jsonString, validator) {
|
|
33
|
-
try {
|
|
34
|
-
const parsed = JSON.parse(jsonString);
|
|
35
|
-
if (validator(parsed)) {
|
|
36
|
-
return parsed;
|
|
37
|
-
}
|
|
38
|
-
console.warn("[Validation] JSON validation failed");
|
|
39
|
-
return null;
|
|
40
|
-
} catch (error) {
|
|
41
|
-
console.error("[Validation] JSON parse error:", error);
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function isValidAccountData(data) {
|
|
46
|
-
return typeof data === "object" && data !== null && typeof data.homeAccountId === "string" && data.homeAccountId.length > 0 && typeof data.username === "string" && data.username.length > 0 && (data.name === void 0 || typeof data.name === "string");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// src/utils/getServerSession.ts
|
|
50
|
-
async function getServerSession() {
|
|
51
|
-
try {
|
|
52
|
-
const cookieStore = await (0, import_headers.cookies)();
|
|
53
|
-
const headersList = await (0, import_headers.headers)();
|
|
54
|
-
const msalAccount = cookieStore.get("msal.account");
|
|
55
|
-
const msalToken = cookieStore.get("msal.token");
|
|
56
|
-
if (msalAccount?.value) {
|
|
57
|
-
const accountData = safeJsonParse(
|
|
58
|
-
msalAccount.value,
|
|
59
|
-
isValidAccountData
|
|
60
|
-
);
|
|
61
|
-
if (accountData) {
|
|
62
|
-
return {
|
|
63
|
-
isAuthenticated: true,
|
|
64
|
-
accountId: accountData.homeAccountId,
|
|
65
|
-
username: accountData.username,
|
|
66
|
-
accessToken: msalToken?.value
|
|
67
|
-
};
|
|
68
|
-
} else {
|
|
69
|
-
console.warn("[ServerSession] Invalid account data in cookie");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const authHeader = headersList.get("x-msal-authenticated");
|
|
73
|
-
if (authHeader === "true") {
|
|
74
|
-
const username = headersList.get("x-msal-username");
|
|
75
|
-
return {
|
|
76
|
-
isAuthenticated: true,
|
|
77
|
-
username: username || void 0
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
isAuthenticated: false
|
|
82
|
-
};
|
|
83
|
-
} catch (error) {
|
|
84
|
-
console.error("[ServerSession] Error reading session:", error);
|
|
85
|
-
return {
|
|
86
|
-
isAuthenticated: false
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async function setServerSessionCookie(account, accessToken) {
|
|
91
|
-
try {
|
|
92
|
-
const accountData = {
|
|
93
|
-
homeAccountId: account.homeAccountId,
|
|
94
|
-
username: account.username,
|
|
95
|
-
name: account.name
|
|
96
|
-
};
|
|
97
|
-
await fetch("/api/auth/session", {
|
|
98
|
-
method: "POST",
|
|
99
|
-
headers: {
|
|
100
|
-
"Content-Type": "application/json"
|
|
101
|
-
},
|
|
102
|
-
body: JSON.stringify({
|
|
103
|
-
account: accountData,
|
|
104
|
-
token: accessToken
|
|
105
|
-
})
|
|
106
|
-
});
|
|
107
|
-
} catch (error) {
|
|
108
|
-
console.error("[ServerSession] Failed to set session cookie:", error);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
-
0 && (module.exports = {
|
|
113
|
-
getServerSession,
|
|
114
|
-
setServerSessionCookie
|
|
115
|
-
});
|
|
1
|
+
'use strict';var headers=require('next/headers');function o(e,r){try{let n=JSON.parse(e);return r(n)?n:(console.warn("[Validation] JSON validation failed"),null)}catch(n){return console.error("[Validation] JSON parse error:",n),null}}function s(e){return typeof e=="object"&&e!==null&&typeof e.homeAccountId=="string"&&e.homeAccountId.length>0&&typeof e.username=="string"&&e.username.length>0&&(e.name===void 0||typeof e.name=="string")}async function u(){try{let e=await headers.cookies(),r=await headers.headers(),n=e.get("msal.account"),a=e.get("msal.token");if(n?.value){let t=o(n.value,s);if(t)return {isAuthenticated:!0,accountId:t.homeAccountId,username:t.username,accessToken:a?.value};console.warn("[ServerSession] Invalid account data in cookie");}return r.get("x-msal-authenticated")==="true"?{isAuthenticated:!0,username:r.get("x-msal-username")||void 0}:{isAuthenticated:!1}}catch(e){return console.error("[ServerSession] Error reading session:",e),{isAuthenticated:false}}}async function l(e,r){try{let n={homeAccountId:e.homeAccountId,username:e.username,name:e.name};await fetch("/api/auth/session",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({account:n,token:r})});}catch(n){console.error("[ServerSession] Failed to set session cookie:",n);}}exports.getServerSession=u;exports.setServerSessionCookie=l;
|
package/dist/server.mjs
CHANGED
|
@@ -1,87 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { cookies, headers } from "next/headers";
|
|
3
|
-
|
|
4
|
-
// src/utils/validation.ts
|
|
5
|
-
function safeJsonParse(jsonString, validator) {
|
|
6
|
-
try {
|
|
7
|
-
const parsed = JSON.parse(jsonString);
|
|
8
|
-
if (validator(parsed)) {
|
|
9
|
-
return parsed;
|
|
10
|
-
}
|
|
11
|
-
console.warn("[Validation] JSON validation failed");
|
|
12
|
-
return null;
|
|
13
|
-
} catch (error) {
|
|
14
|
-
console.error("[Validation] JSON parse error:", error);
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
function isValidAccountData(data) {
|
|
19
|
-
return typeof data === "object" && data !== null && typeof data.homeAccountId === "string" && data.homeAccountId.length > 0 && typeof data.username === "string" && data.username.length > 0 && (data.name === void 0 || typeof data.name === "string");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// src/utils/getServerSession.ts
|
|
23
|
-
async function getServerSession() {
|
|
24
|
-
try {
|
|
25
|
-
const cookieStore = await cookies();
|
|
26
|
-
const headersList = await headers();
|
|
27
|
-
const msalAccount = cookieStore.get("msal.account");
|
|
28
|
-
const msalToken = cookieStore.get("msal.token");
|
|
29
|
-
if (msalAccount?.value) {
|
|
30
|
-
const accountData = safeJsonParse(
|
|
31
|
-
msalAccount.value,
|
|
32
|
-
isValidAccountData
|
|
33
|
-
);
|
|
34
|
-
if (accountData) {
|
|
35
|
-
return {
|
|
36
|
-
isAuthenticated: true,
|
|
37
|
-
accountId: accountData.homeAccountId,
|
|
38
|
-
username: accountData.username,
|
|
39
|
-
accessToken: msalToken?.value
|
|
40
|
-
};
|
|
41
|
-
} else {
|
|
42
|
-
console.warn("[ServerSession] Invalid account data in cookie");
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const authHeader = headersList.get("x-msal-authenticated");
|
|
46
|
-
if (authHeader === "true") {
|
|
47
|
-
const username = headersList.get("x-msal-username");
|
|
48
|
-
return {
|
|
49
|
-
isAuthenticated: true,
|
|
50
|
-
username: username || void 0
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
isAuthenticated: false
|
|
55
|
-
};
|
|
56
|
-
} catch (error) {
|
|
57
|
-
console.error("[ServerSession] Error reading session:", error);
|
|
58
|
-
return {
|
|
59
|
-
isAuthenticated: false
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async function setServerSessionCookie(account, accessToken) {
|
|
64
|
-
try {
|
|
65
|
-
const accountData = {
|
|
66
|
-
homeAccountId: account.homeAccountId,
|
|
67
|
-
username: account.username,
|
|
68
|
-
name: account.name
|
|
69
|
-
};
|
|
70
|
-
await fetch("/api/auth/session", {
|
|
71
|
-
method: "POST",
|
|
72
|
-
headers: {
|
|
73
|
-
"Content-Type": "application/json"
|
|
74
|
-
},
|
|
75
|
-
body: JSON.stringify({
|
|
76
|
-
account: accountData,
|
|
77
|
-
token: accessToken
|
|
78
|
-
})
|
|
79
|
-
});
|
|
80
|
-
} catch (error) {
|
|
81
|
-
console.error("[ServerSession] Failed to set session cookie:", error);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
export {
|
|
85
|
-
getServerSession,
|
|
86
|
-
setServerSessionCookie
|
|
87
|
-
};
|
|
1
|
+
import {cookies,headers}from'next/headers';function o(e,r){try{let n=JSON.parse(e);return r(n)?n:(console.warn("[Validation] JSON validation failed"),null)}catch(n){return console.error("[Validation] JSON parse error:",n),null}}function s(e){return typeof e=="object"&&e!==null&&typeof e.homeAccountId=="string"&&e.homeAccountId.length>0&&typeof e.username=="string"&&e.username.length>0&&(e.name===void 0||typeof e.name=="string")}async function u(){try{let e=await cookies(),r=await headers(),n=e.get("msal.account"),a=e.get("msal.token");if(n?.value){let t=o(n.value,s);if(t)return {isAuthenticated:!0,accountId:t.homeAccountId,username:t.username,accessToken:a?.value};console.warn("[ServerSession] Invalid account data in cookie");}return r.get("x-msal-authenticated")==="true"?{isAuthenticated:!0,username:r.get("x-msal-username")||void 0}:{isAuthenticated:!1}}catch(e){return console.error("[ServerSession] Error reading session:",e),{isAuthenticated:false}}}async function l(e,r){try{let n={homeAccountId:e.homeAccountId,username:e.username,name:e.name};await fetch("/api/auth/session",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({account:n,token:r})});}catch(n){console.error("[ServerSession] Failed to set session cookie:",n);}}export{u as getServerSession,l as setServerSessionCookie};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chemmangat/msal-next",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.8",
|
|
4
4
|
"description": "Production-grade MSAL authentication package for Next.js App Router with minimal boilerplate",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -23,7 +23,8 @@
|
|
|
23
23
|
"CHANGELOG.md",
|
|
24
24
|
"MIGRATION_GUIDE_v3.md",
|
|
25
25
|
"SECURITY.md",
|
|
26
|
-
"TROUBLESHOOTING.md"
|
|
26
|
+
"TROUBLESHOOTING.md",
|
|
27
|
+
"LICENSE"
|
|
27
28
|
],
|
|
28
29
|
"scripts": {
|
|
29
30
|
"build": "tsup",
|