@explita/cloud-auth-client 0.2.0 → 0.2.1
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/lib/utils.d.ts +2 -2
- package/dist/lib/utils.js +3 -3
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/token-session.d.ts +7 -0
- package/dist/server/token-session.js +36 -0
- package/dist/types.d.ts +16 -0
- package/package.json +1 -1
package/dist/lib/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AuthConfig, QueryOpts, User } from "../types";
|
|
1
|
+
import { AuthConfig, QueryOpts, TokenSession, User } from "../types";
|
|
2
2
|
export declare function cn(...classes: (string | false | null | undefined)[]): string;
|
|
3
3
|
export declare function unstuckPointerEvents(): void;
|
|
4
4
|
export declare function buildRouteContext(config?: AuthConfig, currentPath?: string): {
|
|
@@ -13,7 +13,7 @@ export declare function buildRouteContext(config?: AuthConfig, currentPath?: str
|
|
|
13
13
|
isExcluded: boolean;
|
|
14
14
|
};
|
|
15
15
|
export declare function getClientToken(): string;
|
|
16
|
-
export declare function hasPermission(
|
|
16
|
+
export declare function hasPermission(session: User | TokenSession | null, permission: string): boolean;
|
|
17
17
|
export declare function parseMessage(message: string): string;
|
|
18
18
|
export declare function parseGroupId(data?: QueryOpts["groupIds"]): string;
|
|
19
19
|
export declare function parseJwt(token: string): any;
|
package/dist/lib/utils.js
CHANGED
|
@@ -102,10 +102,10 @@ function getClientToken() {
|
|
|
102
102
|
? localStorage.getItem(constants_1.AUTH_TOKEN_KEY) || ""
|
|
103
103
|
: "";
|
|
104
104
|
}
|
|
105
|
-
function hasPermission(
|
|
106
|
-
if (!
|
|
105
|
+
function hasPermission(session, permission) {
|
|
106
|
+
if (!session)
|
|
107
107
|
return false;
|
|
108
|
-
return
|
|
108
|
+
return session.role.permissions?.includes(permission) || session.isSuperAdmin;
|
|
109
109
|
}
|
|
110
110
|
function parseMessage(message) {
|
|
111
111
|
return message == "fetch failed" || message == "Failed to fetch"
|
package/dist/server/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./reset-password";
|
|
|
3
3
|
export * from "./user";
|
|
4
4
|
export * from "./next-cookie-override";
|
|
5
5
|
export * from "./server-session";
|
|
6
|
+
export * from "./token-session";
|
|
6
7
|
export * from "./server-token";
|
|
7
8
|
export * from "./role";
|
|
8
9
|
export { hasPermission } from "../lib/utils";
|
package/dist/server/index.js
CHANGED
|
@@ -20,6 +20,7 @@ __exportStar(require("./reset-password"), exports);
|
|
|
20
20
|
__exportStar(require("./user"), exports);
|
|
21
21
|
__exportStar(require("./next-cookie-override"), exports);
|
|
22
22
|
__exportStar(require("./server-session"), exports);
|
|
23
|
+
__exportStar(require("./token-session"), exports);
|
|
23
24
|
__exportStar(require("./server-token"), exports);
|
|
24
25
|
__exportStar(require("./role"), exports);
|
|
25
26
|
var utils_1 = require("../lib/utils");
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TokenSession } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Reads session from JWT in cookies.
|
|
4
|
+
* No network call. No server validation.
|
|
5
|
+
* Intended for middleware / edge usage.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getTokenSession<T extends Record<string, any>>(): Promise<TokenSession<T> | null>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.getTokenSession = getTokenSession;
|
|
5
|
+
const utils_1 = require("../lib/utils");
|
|
6
|
+
const server_token_1 = require("./server-token");
|
|
7
|
+
/**
|
|
8
|
+
* Reads session from JWT in cookies.
|
|
9
|
+
* No network call. No server validation.
|
|
10
|
+
* Intended for middleware / edge usage.
|
|
11
|
+
*/
|
|
12
|
+
async function getTokenSession() {
|
|
13
|
+
try {
|
|
14
|
+
const token = await (0, server_token_1.getServerToken)();
|
|
15
|
+
if (!token)
|
|
16
|
+
return null;
|
|
17
|
+
const session = (0, utils_1.parseJwt)(token);
|
|
18
|
+
if (!session)
|
|
19
|
+
return null;
|
|
20
|
+
const now = Date.now();
|
|
21
|
+
if (now > session.exp * 1000 - 5000)
|
|
22
|
+
return null; // 5s buffer
|
|
23
|
+
return {
|
|
24
|
+
...session.user,
|
|
25
|
+
meta: {
|
|
26
|
+
sessionId: session.sessionId,
|
|
27
|
+
exp: session.exp,
|
|
28
|
+
iat: session.iat,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
console.error("[getTokenSession] Failed:", err);
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -232,6 +232,22 @@ export type ServerSession = User & {
|
|
|
232
232
|
exp: number;
|
|
233
233
|
};
|
|
234
234
|
};
|
|
235
|
+
export type TokenSession<T extends Record<string, any> = Record<string, any>> = {
|
|
236
|
+
id: string;
|
|
237
|
+
subId: string;
|
|
238
|
+
groupId: string;
|
|
239
|
+
role: {
|
|
240
|
+
label: string;
|
|
241
|
+
permissions: string[];
|
|
242
|
+
};
|
|
243
|
+
isSuperAdmin: boolean;
|
|
244
|
+
metadata: T;
|
|
245
|
+
meta: {
|
|
246
|
+
sessionId: string;
|
|
247
|
+
iat: number;
|
|
248
|
+
exp: number;
|
|
249
|
+
};
|
|
250
|
+
};
|
|
235
251
|
export type QueryOpts = {
|
|
236
252
|
groupIds?: (string | number | null)[];
|
|
237
253
|
};
|