@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 CHANGED
@@ -1,3 +1,5 @@
1
1
  export { useSignIn } from './hooks/useSignIn.ts';
2
2
  export { useSignOut } from './hooks/useSignOut.ts';
3
3
  export { default as withAuth } from './next/proxy.ts';
4
+ export { type AuthOptions } from './types/options.ts';
5
+ export { userHasRole } from './utilities/user.ts';
package/dist/main.js CHANGED
@@ -1,67 +1,83 @@
1
- import { NextResponse as i } from "next/server.js";
2
- import { jwtDecode as f } from "jwt-decode";
3
- import { match as g } from "path-to-regexp";
4
- function y() {
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 S() {
8
+ function T() {
8
9
  throw new Error("useSignOut not yet implementend");
9
10
  }
10
- function p(e) {
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 f(t).roles || null;
18
- } catch (r) {
19
- return console.error(r), null;
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 m = (e, n) => {
23
- const t = `{/:locale}${e}`;
24
- return n ? t : e.replace(/\/\[\[\.\.\.([^\]]+)\]\]/g, "{/*$1}").replace(/\/\[\.\.\.([^\]]+)\]/g, "/*$1").replace(/\[([^\]]+)\]/g, ":$1");
25
- }, a = (e, n) => {
26
- const t = Object.values(e).map(
27
- (r) => g(m(r, n))
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) => t.some((c) => c(r) !== !1);
30
- }, w = (e, n, t) => {
38
+ return (r) => n.some((o) => o(r) !== !1);
39
+ }, x = (t, e, n) => {
31
40
  const r = {
32
- ...n.unauthenticated.routes,
33
- ...n.open.routes
41
+ ...e.unauthenticated.routes,
42
+ ...e.open.routes
34
43
  };
35
- return !a(r, t)(e);
36
- }, R = (e, n, t) => a(
37
- n.unauthenticated.routes,
38
- t
39
- )(e), x = (e, n, t) => a(
40
- n.open.routes,
41
- t
42
- )(e), A = (e, n) => n[e]?.landing || n.default.landing, L = (e, n) => e.filter(
43
- (t) => n.includes(t)
44
- ), O = (e, n) => L(e, n).length > 0;
45
- function $(e) {
46
- return function(t, r) {
47
- const c = t.nextUrl.pathname, { rbac: u, authorizedRoles: d, withLocaleRoutePrefix: l } = e;
48
- if (x(c, u, l))
49
- return r ?? i.next();
50
- const o = p(t.cookies), s = o !== null && o.length > 0, h = o === null || !s ? !1 : O(o, d);
51
- return (!s || !h) && w(c, u, l) ? i.redirect(
52
- new URL(u.default.login, t.url),
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
- ) : s && R(c, u, l) ? o === null || o.length === 0 || o[0] === void 0 ? i.redirect(
55
- new URL(u.default.login, t.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
- ) : i.redirect(
58
- new URL(A(o[0], u), t.url),
72
+ ) : c.redirect(
73
+ new URL(O(u[0], s), n.url),
59
74
  307
60
- ) : r ?? i.next();
75
+ ) : r ?? c.next();
61
76
  };
62
77
  }
63
78
  export {
64
- y as useSignIn,
65
- S as useSignOut,
79
+ I as useSignIn,
80
+ T as useSignOut,
81
+ W as userHasRole,
66
82
  $ as withAuth
67
83
  };
@@ -3,4 +3,5 @@ export type AuthOptions<ValidRoute, AuthorizedRoles extends string[] = []> = {
3
3
  rbac: RbacRoutes<ValidRoute, AuthorizedRoles>;
4
4
  authorizedRoles: AuthorizedRoles;
5
5
  withLocaleRoutePrefix: boolean;
6
+ cookieName?: string;
6
7
  };
@@ -1,9 +1,18 @@
1
1
  import { RequestCookies } from 'next/dist/server/web/spec-extension/cookies.js';
2
2
  /**
3
- * Retrieves the user roles from JWT stored in cookies.
3
+ * Extracts user roles from a JWT token stored in cookies.
4
4
  *
5
- * Note: by reading cookies you will opt out of static rendering for the entire route (group).
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
- * @returns A promise that resolves to the user roles as an array, or null if not found or on error.
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 getRolesFromCookies<AuthorizedRole>(requestCookies: RequestCookies): AuthorizedRole[] | null;
18
+ export declare function getRolesFromRequestCookies<AuthorizedRole>(requestCookies: RequestCookies, cookieName?: string): AuthorizedRole[] | null;
@@ -1 +1,20 @@
1
- export declare const userIsAuthorized: <Role extends string>(userRoles: Role[], authorizedRoles: Role[]) => boolean;
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>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freshheads/react-auth",
3
- "version": "0.0.1-alpha.1",
3
+ "version": "0.0.1-alpha.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "tsc -b && vite build",