@vaadin/hilla-react-auth 24.4.0-alpha14 → 24.4.0-alpha15

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.
@@ -20,6 +20,6 @@ export type RouteObjectWithAuth = IndexRouteObjectWithAuth | NonIndexRouteObject
20
20
  * protected and the user is not authenticated.
21
21
  * @returns the routes extended with protection if needed
22
22
  */
23
- export declare const protectRoutes: (routes: RouteObjectWithAuth[], redirectPath?: string) => RouteObjectWithAuth[];
23
+ export declare function protectRoutes(routes: RouteObjectWithAuth[], redirectPath?: string): RouteObjectWithAuth[];
24
24
  export {};
25
25
  //# sourceMappingURL=ProtectedRoute.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ProtectedRoute.d.ts","sourceRoot":"","sources":["src/ProtectedRoute.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,gBAAgB,EAAY,KAAK,mBAAmB,EAAe,MAAM,kBAAkB,CAAC;AAC1G,OAAO,EAAE,KAAK,WAAW,EAAe,MAAM,cAAc,CAAC;AAE7D,KAAK,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE1C,KAAK,cAAc,GAAG,QAAQ,CAAC;IAAE,MAAM,CAAC,EAAE,WAAW,GAAG,cAAc,CAAA;CAAE,CAAC,CAAC;AAE1E,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;AAE3C,KAAK,wBAAwB,GAAG,QAAQ,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAC;AAC3E,KAAK,2BAA2B,GAAG,QAAQ,CACzC,QAAQ,CAAC,mBAAmB,EAAE,cAAc,CAAC,EAC7C;IACE,QAAQ,CAAC,EAAE,mBAAmB,EAAE,CAAC;CAClC,CACF,CAAC;AACF,MAAM,MAAM,mBAAmB,GAAG,wBAAwB,GAAG,2BAA2B,CAAC;AAsCzF;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,WAChB,mBAAmB,EAAE,iBACf,MAAM,KACnB,mBAAmB,EAmBrB,CAAC"}
1
+ {"version":3,"file":"ProtectedRoute.d.ts","sourceRoot":"","sources":["src/ProtectedRoute.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,gBAAgB,EAAY,KAAK,mBAAmB,EAAe,MAAM,kBAAkB,CAAC;AAC1G,OAAO,EAAE,KAAK,WAAW,EAAe,MAAM,cAAc,CAAC;AAE7D,KAAK,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE1C,KAAK,cAAc,GAAG,QAAQ,CAAC;IAAE,MAAM,CAAC,EAAE,WAAW,GAAG,cAAc,CAAA;CAAE,CAAC,CAAC;AAE1E,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;AAE3C,KAAK,wBAAwB,GAAG,QAAQ,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAC;AAC3E,KAAK,2BAA2B,GAAG,QAAQ,CACzC,QAAQ,CAAC,mBAAmB,EAAE,cAAc,CAAC,EAC7C;IACE,QAAQ,CAAC,EAAE,mBAAmB,EAAE,CAAC;CAClC,CACF,CAAC;AACF,MAAM,MAAM,mBAAmB,GAAG,wBAAwB,GAAG,2BAA2B,CAAC;AAoCzF;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,mBAAmB,EAAE,EAAE,YAAY,GAAE,MAAiB,GAAG,mBAAmB,EAAE,CAiBnH"}
package/ProtectedRoute.js CHANGED
@@ -16,19 +16,16 @@ function ProtectedRoute({ redirectPath, access, element }) {
16
16
  }
17
17
  return element;
18
18
  }
19
- const collectRoutes = (routes) => {
20
- const allRoutes = [];
21
- routes.forEach((route) => {
22
- allRoutes.push(route);
23
- if (route.children !== void 0) {
24
- allRoutes.push(...collectRoutes(route.children));
19
+ function* traverse(routes) {
20
+ for (const route of routes) {
21
+ yield route;
22
+ if (route.children) {
23
+ yield* traverse(route.children);
25
24
  }
26
- });
27
- return allRoutes;
28
- };
29
- const protectRoutes = (routes, redirectPath = "/login") => {
30
- const allRoutes = collectRoutes(routes);
31
- allRoutes.forEach((route) => {
25
+ }
26
+ }
27
+ function protectRoutes(routes, redirectPath = "/login") {
28
+ for (const route of traverse(routes)) {
32
29
  const { handle } = route;
33
30
  const requiresAuth = handle?.requiresLogin ?? handle?.rolesAllowed?.length;
34
31
  if (requiresAuth) {
@@ -41,9 +38,9 @@ const protectRoutes = (routes, redirectPath = "/login") => {
41
38
  }
42
39
  );
43
40
  }
44
- });
41
+ }
45
42
  return routes;
46
- };
43
+ }
47
44
  export {
48
45
  protectRoutes
49
46
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["src/ProtectedRoute.tsx"],
4
- "sourcesContent": ["import { useContext } from 'react';\nimport type { RouteObject } from 'react-router-dom';\nimport { type IndexRouteObject, Navigate, type NonIndexRouteObject, useLocation } from 'react-router-dom';\nimport { type AccessProps, AuthContext } from './useAuth.js';\n\ntype CustomMetadata = Record<string, any>;\n\ntype HandleWithAuth = Readonly<{ handle?: AccessProps & CustomMetadata }>;\n\ntype Override<T, E> = E & Omit<T, keyof E>;\n\ntype IndexRouteObjectWithAuth = Override<IndexRouteObject, HandleWithAuth>;\ntype NonIndexRouteObjectWithAuth = Override<\n Override<NonIndexRouteObject, HandleWithAuth>,\n {\n children?: RouteObjectWithAuth[];\n }\n>;\nexport type RouteObjectWithAuth = IndexRouteObjectWithAuth | NonIndexRouteObjectWithAuth;\n\ninterface ProtectedRouteProps {\n redirectPath: string;\n access: AccessProps;\n element: JSX.Element;\n}\n\nfunction ProtectedRoute({ redirectPath, access, element }: ProtectedRouteProps): JSX.Element | null {\n const {\n state: { initializing, loading, user },\n hasAccess,\n } = useContext(AuthContext);\n\n const location = useLocation();\n\n if (initializing || loading) {\n return <div></div>;\n }\n\n if (!hasAccess(access)) {\n return <Navigate to={redirectPath} state={{ from: location }} replace />;\n }\n\n return element;\n}\n\nconst collectRoutes = <T,>(routes: T[]): T[] => {\n const allRoutes: T[] = [];\n routes.forEach((route) => {\n allRoutes.push(route);\n if ((route as RouteObject).children !== undefined) {\n allRoutes.push(...collectRoutes((route as RouteObject).children as T[]));\n }\n });\n return allRoutes;\n};\n\n/**\n * Adds protection to routes that require authentication.\n * These routes should contain the {@link AccessProps.requiresLogin} and/or\n * {@link AccessProps.rolesAllowed} properties.\n *\n * @param routes - the routes to check if any of them needs to be protected\n * @param redirectPath - the path to redirect to if the route is\n * protected and the user is not authenticated.\n * @returns the routes extended with protection if needed\n */\nexport const protectRoutes = (\n routes: RouteObjectWithAuth[],\n redirectPath: string = '/login',\n): RouteObjectWithAuth[] => {\n const allRoutes: RouteObjectWithAuth[] = collectRoutes(routes);\n\n allRoutes.forEach((route) => {\n const { handle } = route;\n const requiresAuth = handle?.requiresLogin ?? handle?.rolesAllowed?.length;\n\n if (requiresAuth) {\n route.element = (\n <ProtectedRoute\n redirectPath={redirectPath}\n access={route.handle as AccessProps}\n element={route.element as JSX.Element}\n />\n );\n }\n });\n\n return routes;\n};\n"],
5
- "mappings": "AAmCW;AAnCX,SAAS,kBAAkB;AAE3B,SAAgC,UAAoC,mBAAmB;AACvF,SAA2B,mBAAmB;AAuB9C,SAAS,eAAe,EAAE,cAAc,QAAQ,QAAQ,GAA4C;AAClG,QAAM;AAAA,IACJ,OAAO,EAAE,cAAc,SAAS,KAAK;AAAA,IACrC;AAAA,EACF,IAAI,WAAW,WAAW;AAE1B,QAAM,WAAW,YAAY;AAE7B,MAAI,gBAAgB,SAAS;AAC3B,WAAO,oBAAC,SAAI;AAAA,EACd;AAEA,MAAI,CAAC,UAAU,MAAM,GAAG;AACtB,WAAO,oBAAC,YAAS,IAAI,cAAc,OAAO,EAAE,MAAM,SAAS,GAAG,SAAO,MAAC;AAAA,EACxE;AAEA,SAAO;AACT;AAEA,MAAM,gBAAgB,CAAK,WAAqB;AAC9C,QAAM,YAAiB,CAAC;AACxB,SAAO,QAAQ,CAAC,UAAU;AACxB,cAAU,KAAK,KAAK;AACpB,QAAK,MAAsB,aAAa,QAAW;AACjD,gBAAU,KAAK,GAAG,cAAe,MAAsB,QAAe,CAAC;AAAA,IACzE;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAYO,MAAM,gBAAgB,CAC3B,QACA,eAAuB,aACG;AAC1B,QAAM,YAAmC,cAAc,MAAM;AAE7D,YAAU,QAAQ,CAAC,UAAU;AAC3B,UAAM,EAAE,OAAO,IAAI;AACnB,UAAM,eAAe,QAAQ,iBAAiB,QAAQ,cAAc;AAEpE,QAAI,cAAc;AAChB,YAAM,UACJ;AAAA,QAAC;AAAA;AAAA,UACC;AAAA,UACA,QAAQ,MAAM;AAAA,UACd,SAAS,MAAM;AAAA;AAAA,MACjB;AAAA,IAEJ;AAAA,EACF,CAAC;AAED,SAAO;AACT;",
4
+ "sourcesContent": ["import { useContext, type JSX } from 'react';\nimport type { RouteObject } from 'react-router-dom';\nimport { type IndexRouteObject, Navigate, type NonIndexRouteObject, useLocation } from 'react-router-dom';\nimport { type AccessProps, AuthContext } from './useAuth.js';\n\ntype CustomMetadata = Record<string, any>;\n\ntype HandleWithAuth = Readonly<{ handle?: AccessProps & CustomMetadata }>;\n\ntype Override<T, E> = E & Omit<T, keyof E>;\n\ntype IndexRouteObjectWithAuth = Override<IndexRouteObject, HandleWithAuth>;\ntype NonIndexRouteObjectWithAuth = Override<\n Override<NonIndexRouteObject, HandleWithAuth>,\n {\n children?: RouteObjectWithAuth[];\n }\n>;\nexport type RouteObjectWithAuth = IndexRouteObjectWithAuth | NonIndexRouteObjectWithAuth;\n\ninterface ProtectedRouteProps {\n redirectPath: string;\n access: AccessProps;\n element: JSX.Element;\n}\n\nfunction ProtectedRoute({ redirectPath, access, element }: ProtectedRouteProps): JSX.Element | null {\n const {\n state: { initializing, loading, user },\n hasAccess,\n } = useContext(AuthContext);\n\n const location = useLocation();\n\n if (initializing || loading) {\n return <div></div>;\n }\n\n if (!hasAccess(access)) {\n return <Navigate to={redirectPath} state={{ from: location }} replace />;\n }\n\n return element;\n}\n\nfunction* traverse<T extends RouteObject>(routes: T[]): Generator<T, undefined, undefined> {\n for (const route of routes) {\n yield route;\n if (route.children) {\n yield* traverse(route.children as T[]);\n }\n }\n}\n\n/**\n * Adds protection to routes that require authentication.\n * These routes should contain the {@link AccessProps.requiresLogin} and/or\n * {@link AccessProps.rolesAllowed} properties.\n *\n * @param routes - the routes to check if any of them needs to be protected\n * @param redirectPath - the path to redirect to if the route is\n * protected and the user is not authenticated.\n * @returns the routes extended with protection if needed\n */\nexport function protectRoutes(routes: RouteObjectWithAuth[], redirectPath: string = '/login'): RouteObjectWithAuth[] {\n for (const route of traverse(routes)) {\n const { handle } = route;\n const requiresAuth = handle?.requiresLogin ?? handle?.rolesAllowed?.length;\n\n if (requiresAuth) {\n route.element = (\n <ProtectedRoute\n redirectPath={redirectPath}\n access={route.handle as AccessProps}\n element={route.element as JSX.Element}\n />\n );\n }\n }\n\n return routes;\n}\n"],
5
+ "mappings": "AAmCW;AAnCX,SAAS,kBAA4B;AAErC,SAAgC,UAAoC,mBAAmB;AACvF,SAA2B,mBAAmB;AAuB9C,SAAS,eAAe,EAAE,cAAc,QAAQ,QAAQ,GAA4C;AAClG,QAAM;AAAA,IACJ,OAAO,EAAE,cAAc,SAAS,KAAK;AAAA,IACrC;AAAA,EACF,IAAI,WAAW,WAAW;AAE1B,QAAM,WAAW,YAAY;AAE7B,MAAI,gBAAgB,SAAS;AAC3B,WAAO,oBAAC,SAAI;AAAA,EACd;AAEA,MAAI,CAAC,UAAU,MAAM,GAAG;AACtB,WAAO,oBAAC,YAAS,IAAI,cAAc,OAAO,EAAE,MAAM,SAAS,GAAG,SAAO,MAAC;AAAA,EACxE;AAEA,SAAO;AACT;AAEA,UAAU,SAAgC,QAAiD;AACzF,aAAW,SAAS,QAAQ;AAC1B,UAAM;AACN,QAAI,MAAM,UAAU;AAClB,aAAO,SAAS,MAAM,QAAe;AAAA,IACvC;AAAA,EACF;AACF;AAYO,SAAS,cAAc,QAA+B,eAAuB,UAAiC;AACnH,aAAW,SAAS,SAAS,MAAM,GAAG;AACpC,UAAM,EAAE,OAAO,IAAI;AACnB,UAAM,eAAe,QAAQ,iBAAiB,QAAQ,cAAc;AAEpE,QAAI,cAAc;AAChB,YAAM,UACJ;AAAA,QAAC;AAAA;AAAA,UACC;AAAA,UACA,QAAQ,MAAM;AAAA,UACd,SAAS,MAAM;AAAA;AAAA,MACjB;AAAA,IAEJ;AAAA,EACF;AAEA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
package/index.js CHANGED
@@ -3,7 +3,7 @@ function __REGISTER__(feature) {
3
3
  window.Vaadin.registrations ??= [];
4
4
  window.Vaadin.registrations.push({
5
5
  is: feature ? `${"@vaadin/hilla-react-auth"}/${feature}` : "@vaadin/hilla-react-auth",
6
- version: "24.4.0-alpha14"
6
+ version: "24.4.0-alpha15"
7
7
  });
8
8
  }
9
9
  export * from "./ProtectedRoute.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/hilla-react-auth",
3
- "version": "24.4.0-alpha14",
3
+ "version": "24.4.0-alpha15",
4
4
  "description": "Hilla auth utils for React",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -46,7 +46,7 @@
46
46
  "access": "public"
47
47
  },
48
48
  "dependencies": {
49
- "@vaadin/hilla-frontend": "24.4.0-alpha14"
49
+ "@vaadin/hilla-frontend": "24.4.0-alpha15"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "react": "^18",