@freshheads/react-auth 0.0.1-alpha.4 → 0.0.1-alpha.5
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/hooks/useUser.d.ts +4 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +47 -34
- package/package.json +1 -1
package/dist/main.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { useSignIn } from './hooks/useSignIn.ts';
|
|
2
2
|
export { useSignOut } from './hooks/useSignOut.ts';
|
|
3
|
+
export { useUser } from './hooks/useUser.ts';
|
|
3
4
|
export { default as withAuth } from './next/proxy.ts';
|
|
4
5
|
export { type AuthOptions } from './types/options.ts';
|
|
5
6
|
export { userHasRole } from './utilities/user.ts';
|
package/dist/main.js
CHANGED
|
@@ -1,85 +1,98 @@
|
|
|
1
|
+
import { useState as R, useEffect as w } from "react";
|
|
2
|
+
import { jwtDecode as k } from "jwt-decode";
|
|
3
|
+
import { cookies as A } from "next/headers";
|
|
1
4
|
import { NextResponse as c } from "next/server.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { match as k } from "path-to-regexp";
|
|
5
|
-
function I() {
|
|
5
|
+
import { match as x } from "path-to-regexp";
|
|
6
|
+
function $() {
|
|
6
7
|
throw new Error("useSignIn not yet implementend");
|
|
7
8
|
}
|
|
8
|
-
function
|
|
9
|
+
function j() {
|
|
9
10
|
throw new Error("useSignOut not yet implementend");
|
|
10
11
|
}
|
|
11
|
-
const
|
|
12
|
+
const f = "jwt_hp", h = (t) => {
|
|
12
13
|
try {
|
|
13
|
-
return
|
|
14
|
+
return k(t);
|
|
14
15
|
} catch (e) {
|
|
15
16
|
return console.error(e), null;
|
|
16
17
|
}
|
|
17
|
-
},
|
|
18
|
+
}, d = async (t) => {
|
|
18
19
|
"use server";
|
|
19
|
-
const e = await
|
|
20
|
+
const e = await A(), n = t || f;
|
|
20
21
|
if (!e.has(n))
|
|
21
22
|
return console.error("No cookie found with name:", n), null;
|
|
22
23
|
const o = e.get(n)?.value;
|
|
23
|
-
return o ?
|
|
24
|
+
return o ? h(o)?.roles || null : (console.error("No JWT found in cookies"), null);
|
|
24
25
|
};
|
|
25
|
-
function
|
|
26
|
-
const n = e ||
|
|
26
|
+
function y(t, e) {
|
|
27
|
+
const n = e || f;
|
|
27
28
|
if (!t.has(n))
|
|
28
29
|
return console.error("No cookie found with name:", n), null;
|
|
29
30
|
const o = t.get(n)?.value;
|
|
30
|
-
return o ?
|
|
31
|
+
return o ? h(o)?.roles || null : (console.error("No JWT found in cookies"), null);
|
|
31
32
|
}
|
|
32
|
-
const
|
|
33
|
+
const z = (t) => {
|
|
34
|
+
const [e, n] = R(null);
|
|
35
|
+
return w(() => {
|
|
36
|
+
(async () => {
|
|
37
|
+
const s = await d(t);
|
|
38
|
+
n(s);
|
|
39
|
+
})();
|
|
40
|
+
}, [t]), {
|
|
41
|
+
roles: e,
|
|
42
|
+
hasRole: (o) => e === null ? !1 : e.includes(o)
|
|
43
|
+
};
|
|
44
|
+
}, v = (t, e) => {
|
|
33
45
|
const n = `{/:locale}${t}`;
|
|
34
46
|
return e ? n : t.replace(/\/\[\[\.\.\.([^\]]+)\]\]/g, "{/*$1}").replace(/\/\[\.\.\.([^\]]+)\]/g, "/*$1").replace(/\[([^\]]+)\]/g, ":$1");
|
|
35
47
|
}, a = (t, e) => {
|
|
36
48
|
const n = Object.values(t).map(
|
|
37
|
-
(r) =>
|
|
49
|
+
(r) => x(v(r, e))
|
|
38
50
|
);
|
|
39
51
|
return (r) => n.some((o) => o(r) !== !1);
|
|
40
|
-
},
|
|
52
|
+
}, L = (t, e, n) => {
|
|
41
53
|
const r = {
|
|
42
54
|
...e.unauthenticated.routes,
|
|
43
55
|
...e.open.routes
|
|
44
56
|
};
|
|
45
57
|
return !a(r, n)(t);
|
|
46
|
-
},
|
|
58
|
+
}, N = (t, e, n) => a(
|
|
47
59
|
e.unauthenticated.routes,
|
|
48
60
|
n
|
|
49
|
-
)(t),
|
|
61
|
+
)(t), O = (t, e, n) => a(
|
|
50
62
|
e.open.routes,
|
|
51
63
|
n
|
|
52
|
-
)(t),
|
|
64
|
+
)(t), U = (t, e) => e[t]?.landing || e.default.landing, E = (t, e) => t.filter(
|
|
53
65
|
(n) => e.includes(n)
|
|
54
|
-
), J = async (t, e) =>
|
|
66
|
+
), J = async (t, e) => E(t, e).length > 0, F = async (t) => {
|
|
55
67
|
"use server";
|
|
56
|
-
const e = await
|
|
68
|
+
const e = await d();
|
|
57
69
|
return e === null ? !1 : e.includes(t);
|
|
58
70
|
};
|
|
59
|
-
function
|
|
71
|
+
function _(t) {
|
|
60
72
|
return function(n, r) {
|
|
61
|
-
const o = n.nextUrl.pathname, { rbac: s, authorizedRoles:
|
|
62
|
-
if (
|
|
73
|
+
const o = n.nextUrl.pathname, { rbac: s, authorizedRoles: m, withLocaleRoutePrefix: l, cookieName: g } = t;
|
|
74
|
+
if (O(o, s, l))
|
|
63
75
|
return r ?? c.next();
|
|
64
|
-
const u =
|
|
76
|
+
const u = y(
|
|
65
77
|
n.cookies,
|
|
66
|
-
|
|
67
|
-
),
|
|
68
|
-
return (!
|
|
78
|
+
g
|
|
79
|
+
), i = u !== null && u.length > 0, p = u === null || !i ? !1 : J(u, m);
|
|
80
|
+
return (!i || !p) && L(o, s, l) ? c.redirect(
|
|
69
81
|
new URL(s.default.login, n.url),
|
|
70
82
|
307
|
|
71
|
-
) :
|
|
83
|
+
) : i && N(o, s, l) ? u === null || u.length === 0 || u[0] === void 0 ? c.redirect(
|
|
72
84
|
new URL(s.default.login, n.url),
|
|
73
85
|
307
|
|
74
86
|
) : c.redirect(
|
|
75
|
-
new URL(
|
|
87
|
+
new URL(U(u[0], s), n.url),
|
|
76
88
|
307
|
|
77
89
|
) : r ?? c.next();
|
|
78
90
|
};
|
|
79
91
|
}
|
|
80
92
|
export {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
93
|
+
$ as useSignIn,
|
|
94
|
+
j as useSignOut,
|
|
95
|
+
z as useUser,
|
|
96
|
+
F as userHasRole,
|
|
97
|
+
_ as withAuth
|
|
85
98
|
};
|