authera 2.2.2 → 2.2.3

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.
@@ -12,24 +12,8 @@ export default function createAxios(storage, settings) {
12
12
  });
13
13
  // Single-flight refresh across concurrent 401s
14
14
  let refreshPromise = null;
15
- const readAccessToken = () => {
16
- try {
17
- const token = storage.get("access_token");
18
- return token || null;
19
- }
20
- catch {
21
- return null;
22
- }
23
- };
24
- const readRefreshToken = () => {
25
- try {
26
- const token = storage.get("refresh_token");
27
- return token || null;
28
- }
29
- catch {
30
- return null;
31
- }
32
- };
15
+ const accessToken = storage.get("access_token").replaceAll('"', "");
16
+ const refreshToken = storage.get("refresh_token").replaceAll('"', "");
33
17
  const writeTokens = (access, refresh) => {
34
18
  if (access)
35
19
  storage.set("access_token", access);
@@ -43,13 +27,12 @@ export default function createAxios(storage, settings) {
43
27
  return data?.refresh_token ?? data?.refreshToken ?? null;
44
28
  };
45
29
  const doRefresh = async () => {
46
- const token = readRefreshToken();
47
- if (!token)
48
- return null;
49
30
  try {
50
31
  // Use a bare client to avoid interceptor recursion
51
32
  const client = axios.create({ baseURL: settings.backendUrl });
52
- const resp = await client.post("refresh/", { refresh_token: token });
33
+ const resp = await client.post("refresh/", {
34
+ refresh_token: refreshToken,
35
+ });
53
36
  const newAccess = resolveAccessFromResponse(resp.data);
54
37
  const newRefresh = resolveRefreshFromResponse(resp.data);
55
38
  writeTokens(newAccess, newRefresh);
@@ -70,13 +53,10 @@ export default function createAxios(storage, settings) {
70
53
  };
71
54
  // Attach Authorization header on each request
72
55
  instance.interceptors.request.use((config) => {
73
- const access = readAccessToken();
74
- if (access) {
75
- config.headers = {
76
- ...config.headers,
77
- Authorization: `Bearer ${access}`,
78
- };
79
- }
56
+ config.headers = {
57
+ ...config.headers,
58
+ Authorization: `Bearer ${accessToken}`,
59
+ };
80
60
  return config;
81
61
  });
82
62
  // Handle 401 responses with token refresh
@@ -4,4 +4,4 @@ export interface AuthGuardProps {
4
4
  permits: string[];
5
5
  action: "show" | "hide" | "redirect";
6
6
  }
7
- export default function AuthGuard({ children, permits, action, }: AuthGuardProps): void;
7
+ export default function AuthGuard({ children, permits, action, }: AuthGuardProps): import("react/jsx-runtime").JSX.Element | null | undefined;
package/dist/web/guard.js CHANGED
@@ -1,18 +1,23 @@
1
1
  "use client";
2
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
+ import { useAuth } from "../hooks/useAuth";
2
4
  export default function AuthGuard({ children, permits, action, }) {
3
- // const { isPermittedAll: isPermittedHook, fallback_401_url } =
4
- // useAuth<string>();
5
- // const isPermitted = isPermittedHook(permits);
6
- // if (action === "redirect" && !isPermitted) {
7
- // window.location.href = fallback_401_url;
8
- // return null;
9
- // }
10
- // if (action === "show") {
11
- // if (isPermitted) return <>{children}</>;
12
- // else return null;
13
- // }
14
- // if (action === "hide") {
15
- // if (isPermitted) return null;
16
- // else return <>{children}</>;
17
- // }
5
+ const { isPermittedAll: isPermittedHook, fallback_401_url } = useAuth();
6
+ const isPermitted = isPermittedHook(permits);
7
+ if (action === "redirect" && !isPermitted) {
8
+ window.location.href = fallback_401_url;
9
+ return null;
10
+ }
11
+ if (action === "show") {
12
+ if (isPermitted)
13
+ return _jsx(_Fragment, { children: children });
14
+ else
15
+ return null;
16
+ }
17
+ if (action === "hide") {
18
+ if (isPermitted)
19
+ return null;
20
+ else
21
+ return _jsx(_Fragment, { children: children });
22
+ }
18
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authera",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "this project is a simple auth hook for react",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -43,4 +43,4 @@
43
43
  "minimal-form": "^2.8.1",
44
44
  "react-hook-form": "^7.66.0"
45
45
  }
46
- }
46
+ }