@bouko/react 2.9.4 → 2.9.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.
@@ -6,6 +6,7 @@ export { default as Heading } from "./heading/normal";
6
6
  export { default as PageHeading } from "./heading/page";
7
7
  export * from "./layout/flex";
8
8
  export * from "./tab";
9
+ export * from "./route";
9
10
  export { default as Pagination } from "./pagination";
10
11
  export { default as Button } from "./button/normal";
11
12
  export { default as IconButton } from "./button/icon";
@@ -6,6 +6,7 @@ export { default as Heading } from "./heading/normal";
6
6
  export { default as PageHeading } from "./heading/page";
7
7
  export * from "./layout/flex";
8
8
  export * from "./tab";
9
+ export * from "./route";
9
10
  export { default as Pagination } from "./pagination";
10
11
  export { default as Button } from "./button/normal";
11
12
  export { default as IconButton } from "./button/icon";
@@ -0,0 +1,7 @@
1
+ type Props = {
2
+ loading: boolean;
3
+ authed: boolean;
4
+ children: React.ReactNode;
5
+ };
6
+ export declare function ProtectedRoute({ loading, authed, children }: Props): import("react").ReactNode;
7
+ export {};
@@ -0,0 +1,11 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useLocation, Navigate } from "react-router-dom";
3
+ export function ProtectedRoute({ loading, authed, children }) {
4
+ const location = useLocation();
5
+ if (loading)
6
+ return null;
7
+ if (!authed)
8
+ return;
9
+ _jsx(Navigate, { to: "/", state: { from: location }, replace: true });
10
+ return children;
11
+ }
@@ -1,2 +1,3 @@
1
1
  export { AuthProvider } from "./provider";
2
2
  export { useAuth } from "./context";
3
+ export * from "./redirect";
@@ -1,2 +1,3 @@
1
1
  export { AuthProvider } from "./provider";
2
2
  export { useAuth } from "./context";
3
+ export * from "./redirect";
@@ -0,0 +1,6 @@
1
+ type Props = {
2
+ checker: () => Promise<boolean>;
3
+ to: string;
4
+ };
5
+ export declare function useAuthRedirect({ checker, to }: Props): void;
6
+ export {};
@@ -0,0 +1,15 @@
1
+ import { useEffect } from "react";
2
+ import { useNavigate } from "react-router-dom";
3
+ import { useAuth } from "./context";
4
+ export function useAuthRedirect({ checker, to }) {
5
+ const navigate = useNavigate();
6
+ const { user } = useAuth();
7
+ useEffect(() => {
8
+ const checkAuth = async () => {
9
+ const ok = await checker();
10
+ if (ok)
11
+ navigate(to);
12
+ };
13
+ checkAuth();
14
+ }, [user]);
15
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
 
3
3
  "name": "@bouko/react",
4
- "version": "2.9.4",
4
+ "version": "2.9.6",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "license": "MIT",
@@ -13,6 +13,9 @@
13
13
  },
14
14
  "author": "",
15
15
  "description": "",
16
+ "peerDependencies": {
17
+ "react-router-dom": "^7.12.0"
18
+ },
16
19
  "engines": {},
17
20
 
18
21
  "scripts": {