@freshheads/react-auth 0.0.1-alpha.1 → 0.0.1-alpha.2
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/main.d.ts +2 -0
- package/dist/main.js +65 -49
- package/dist/types/options.d.ts +1 -0
- package/dist/utilities/cookies.d.ts +13 -4
- package/dist/utilities/user.d.ts +20 -1
- package/package.json +1 -1
package/dist/main.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -1,67 +1,83 @@
|
|
|
1
|
-
import { NextResponse as
|
|
2
|
-
import { jwtDecode as
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { NextResponse as c } from "next/server.js";
|
|
2
|
+
import { jwtDecode as p } from "jwt-decode";
|
|
3
|
+
import { cookies as w } from "next/headers";
|
|
4
|
+
import { match as k } from "path-to-regexp";
|
|
5
|
+
function I() {
|
|
5
6
|
throw new Error("useSignIn not yet implementend");
|
|
6
7
|
}
|
|
7
|
-
function
|
|
8
|
+
function T() {
|
|
8
9
|
throw new Error("useSignOut not yet implementend");
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
if (!e.has("jwt_hp"))
|
|
12
|
-
return null;
|
|
13
|
-
const t = e.get("jwt_hp")?.value;
|
|
14
|
-
if (!t)
|
|
15
|
-
return null;
|
|
11
|
+
const h = "jwt_hp", f = (t) => {
|
|
16
12
|
try {
|
|
17
|
-
return
|
|
18
|
-
} catch (
|
|
19
|
-
return console.error(
|
|
13
|
+
return p(t);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
return console.error(e), null;
|
|
20
16
|
}
|
|
17
|
+
}, R = async (t) => {
|
|
18
|
+
const e = await w(), n = h;
|
|
19
|
+
if (!e.has(n))
|
|
20
|
+
return console.error("No cookie found with name:", n), null;
|
|
21
|
+
const o = e.get(n)?.value;
|
|
22
|
+
return o ? f(o)?.roles || null : (console.error("No JWT found in cookies"), null);
|
|
23
|
+
};
|
|
24
|
+
function A(t, e) {
|
|
25
|
+
const n = e || h;
|
|
26
|
+
if (!t.has(n))
|
|
27
|
+
return console.error("No cookie found with name:", n), null;
|
|
28
|
+
const o = t.get(n)?.value;
|
|
29
|
+
return o ? f(o)?.roles || null : (console.error("No JWT found in cookies"), null);
|
|
21
30
|
}
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
return
|
|
25
|
-
}, a = (
|
|
26
|
-
const
|
|
27
|
-
(r) =>
|
|
31
|
+
const N = (t, e) => {
|
|
32
|
+
const n = `{/:locale}${t}`;
|
|
33
|
+
return e ? n : t.replace(/\/\[\[\.\.\.([^\]]+)\]\]/g, "{/*$1}").replace(/\/\[\.\.\.([^\]]+)\]/g, "/*$1").replace(/\[([^\]]+)\]/g, ":$1");
|
|
34
|
+
}, a = (t, e) => {
|
|
35
|
+
const n = Object.values(t).map(
|
|
36
|
+
(r) => k(N(r, e))
|
|
28
37
|
);
|
|
29
|
-
return (r) =>
|
|
30
|
-
},
|
|
38
|
+
return (r) => n.some((o) => o(r) !== !1);
|
|
39
|
+
}, x = (t, e, n) => {
|
|
31
40
|
const r = {
|
|
32
|
-
...
|
|
33
|
-
...
|
|
41
|
+
...e.unauthenticated.routes,
|
|
42
|
+
...e.open.routes
|
|
34
43
|
};
|
|
35
|
-
return !a(r,
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
)(
|
|
43
|
-
(
|
|
44
|
-
),
|
|
45
|
-
|
|
46
|
-
return
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const o =
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
return !a(r, n)(t);
|
|
45
|
+
}, y = (t, e, n) => a(
|
|
46
|
+
e.unauthenticated.routes,
|
|
47
|
+
n
|
|
48
|
+
)(t), L = (t, e, n) => a(
|
|
49
|
+
e.open.routes,
|
|
50
|
+
n
|
|
51
|
+
)(t), O = (t, e) => e[t]?.landing || e.default.landing, J = (t, e) => t.filter(
|
|
52
|
+
(n) => e.includes(n)
|
|
53
|
+
), U = async (t, e) => J(t, e).length > 0, W = async (t) => {
|
|
54
|
+
const e = await R();
|
|
55
|
+
return e === null ? !1 : e.includes(t);
|
|
56
|
+
};
|
|
57
|
+
function $(t) {
|
|
58
|
+
return function(n, r) {
|
|
59
|
+
const o = n.nextUrl.pathname, { rbac: s, authorizedRoles: d, withLocaleRoutePrefix: i, cookieName: m } = t;
|
|
60
|
+
if (L(o, s, i))
|
|
61
|
+
return r ?? c.next();
|
|
62
|
+
const u = A(
|
|
63
|
+
n.cookies,
|
|
64
|
+
m
|
|
65
|
+
), l = u !== null && u.length > 0, g = u === null || !l ? !1 : U(u, d);
|
|
66
|
+
return (!l || !g) && x(o, s, i) ? c.redirect(
|
|
67
|
+
new URL(s.default.login, n.url),
|
|
53
68
|
307
|
|
54
|
-
) :
|
|
55
|
-
new URL(
|
|
69
|
+
) : l && y(o, s, i) ? u === null || u.length === 0 || u[0] === void 0 ? c.redirect(
|
|
70
|
+
new URL(s.default.login, n.url),
|
|
56
71
|
307
|
|
57
|
-
) :
|
|
58
|
-
new URL(
|
|
72
|
+
) : c.redirect(
|
|
73
|
+
new URL(O(u[0], s), n.url),
|
|
59
74
|
307
|
|
60
|
-
) : r ??
|
|
75
|
+
) : r ?? c.next();
|
|
61
76
|
};
|
|
62
77
|
}
|
|
63
78
|
export {
|
|
64
|
-
|
|
65
|
-
|
|
79
|
+
I as useSignIn,
|
|
80
|
+
T as useSignOut,
|
|
81
|
+
W as userHasRole,
|
|
66
82
|
$ as withAuth
|
|
67
83
|
};
|
package/dist/types/options.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { RequestCookies } from 'next/dist/server/web/spec-extension/cookies.js';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Extracts user roles from a JWT token stored in cookies.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* @template AuthorizedRole - The type of role objects in the roles array
|
|
6
|
+
* @param [cookieName='jwt_hp'] - Name of the cookie containing the JWT token
|
|
7
|
+
* @returns Array of user roles, or null if cookie is missing or invalid
|
|
8
|
+
*/
|
|
9
|
+
export declare const getRolesFromCookies: <AuthorizedRole>(cookieName?: string) => Promise<AuthorizedRole[] | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Extracts user roles from a JWT token stored in request cookies.
|
|
6
12
|
*
|
|
7
|
-
* @
|
|
13
|
+
* @template AuthorizedRole - The type of role objects in the roles array
|
|
14
|
+
* @param requestCookies - Next.js request cookies object
|
|
15
|
+
* @param [cookieName='jwt_hp'] - Name of the cookie containing the JWT token
|
|
16
|
+
* @returns Array of user roles, or null if cookie is missing or invalid
|
|
8
17
|
*/
|
|
9
|
-
export declare function
|
|
18
|
+
export declare function getRolesFromRequestCookies<AuthorizedRole>(requestCookies: RequestCookies, cookieName?: string): AuthorizedRole[] | null;
|
package/dist/utilities/user.d.ts
CHANGED
|
@@ -1 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a user is authorized based on their roles.
|
|
3
|
+
* Used primarily in Next.js middleware for role-based access control (RBAC).
|
|
4
|
+
* Returns true if the user has at least one role that matches the authorized roles.
|
|
5
|
+
*
|
|
6
|
+
* @template Role - The type of role string
|
|
7
|
+
* @param userRoles - Array of roles assigned to the user
|
|
8
|
+
* @param authorizedRoles - Array of roles that are authorized for the resource
|
|
9
|
+
* @returns True if the user has at least one authorized role, false otherwise
|
|
10
|
+
*/
|
|
11
|
+
export declare const userIsAuthorized: <Role extends string>(userRoles: Role[], authorizedRoles: Role[]) => Promise<boolean>;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the current user has a specific role.
|
|
14
|
+
* This function will make a request to the cookies store to retrieve the users roles.
|
|
15
|
+
*
|
|
16
|
+
* @template Role - The type of role string
|
|
17
|
+
* @param requiredRole - The role to check for
|
|
18
|
+
* @returns True if the user has the required role, false otherwise
|
|
19
|
+
*/
|
|
20
|
+
export declare const userHasRole: <Role extends string>(requiredRole: Role) => Promise<boolean>;
|