@freshheads/react-auth 0.0.1-alpha.4 → 0.0.1-alpha.6

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