authera 2.1.0 → 2.2.1
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/helper/axios.d.ts +10 -0
- package/dist/helper/axios.js +88 -0
- package/dist/hooks/useAuth.d.ts +2 -0
- package/dist/hooks/useAuth.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/web/login.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type AxiosInstance } from "axios";
|
|
2
|
+
import type { customeFunc } from "./storage";
|
|
3
|
+
import type { AuthHookSettings } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* Create a preconfigured Axios instance that:
|
|
6
|
+
* - Attaches Authorization header from storage on each request
|
|
7
|
+
* - On 401 responses, attempts token refresh and retries the original request
|
|
8
|
+
* - If refresh fails, redirects to fallback_401_url
|
|
9
|
+
*/
|
|
10
|
+
export default function createAxios<T extends string>(storage: customeFunc, settings: AuthHookSettings<T>): AxiosInstance;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { useAuth } from "../hooks/useAuth";
|
|
4
|
+
/**
|
|
5
|
+
* Create a preconfigured Axios instance that:
|
|
6
|
+
* - Attaches Authorization header from storage on each request
|
|
7
|
+
* - On 401 responses, attempts token refresh and retries the original request
|
|
8
|
+
* - If refresh fails, redirects to fallback_401_url
|
|
9
|
+
*/
|
|
10
|
+
export default function createAxios(storage, settings) {
|
|
11
|
+
const { setAccessToken, setRefreshToken, access_token, refresh_token } = useAuth();
|
|
12
|
+
const instance = axios.create({
|
|
13
|
+
baseURL: settings.backendUrl,
|
|
14
|
+
});
|
|
15
|
+
// Single-flight refresh across concurrent 401s
|
|
16
|
+
let refreshPromise = null;
|
|
17
|
+
const writeTokens = (access, refresh) => {
|
|
18
|
+
if (access)
|
|
19
|
+
setAccessToken(access);
|
|
20
|
+
if (refresh)
|
|
21
|
+
setRefreshToken(refresh);
|
|
22
|
+
};
|
|
23
|
+
const resolveAccessFromResponse = (data) => {
|
|
24
|
+
return data?.access_token ?? data?.accessToken ?? data?.token ?? null;
|
|
25
|
+
};
|
|
26
|
+
const resolveRefreshFromResponse = (data) => {
|
|
27
|
+
return data?.refresh_token ?? data?.refreshToken ?? null;
|
|
28
|
+
};
|
|
29
|
+
const doRefresh = async () => {
|
|
30
|
+
const token = refresh_token;
|
|
31
|
+
if (!token)
|
|
32
|
+
return null;
|
|
33
|
+
try {
|
|
34
|
+
// Use a bare client to avoid interceptor recursion
|
|
35
|
+
const client = axios.create({ baseURL: settings.backendUrl });
|
|
36
|
+
const resp = await client.post("auth/refresh/", { refresh_token: token });
|
|
37
|
+
const newAccess = resolveAccessFromResponse(resp.data);
|
|
38
|
+
const newRefresh = resolveRefreshFromResponse(resp.data);
|
|
39
|
+
writeTokens(newAccess, newRefresh);
|
|
40
|
+
return newAccess;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const getOrStartRefresh = () => {
|
|
47
|
+
if (!refreshPromise) {
|
|
48
|
+
refreshPromise = doRefresh().finally(() => {
|
|
49
|
+
// allow subsequent refreshes
|
|
50
|
+
refreshPromise = null;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return refreshPromise;
|
|
54
|
+
};
|
|
55
|
+
// Attach Authorization header on each request
|
|
56
|
+
instance.interceptors.request.use((config) => {
|
|
57
|
+
if (access_token) {
|
|
58
|
+
config.headers = {
|
|
59
|
+
...config.headers,
|
|
60
|
+
Authorization: `Bearer ${access_token}`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return config;
|
|
64
|
+
});
|
|
65
|
+
// Handle 401 responses with token refresh
|
|
66
|
+
instance.interceptors.response.use((response) => response, async (error) => {
|
|
67
|
+
const responseStatus = error.response?.status;
|
|
68
|
+
const originalConfig = (error.config || {});
|
|
69
|
+
if (responseStatus === 401 && !originalConfig._retry) {
|
|
70
|
+
originalConfig._retry = true;
|
|
71
|
+
const newAccess = await getOrStartRefresh();
|
|
72
|
+
if (newAccess) {
|
|
73
|
+
originalConfig.headers = {
|
|
74
|
+
...(originalConfig.headers || {}),
|
|
75
|
+
Authorization: `Bearer ${newAccess}`,
|
|
76
|
+
};
|
|
77
|
+
// retry original request with new token
|
|
78
|
+
return instance.request(originalConfig);
|
|
79
|
+
}
|
|
80
|
+
// refresh failed: redirect to login/fallback
|
|
81
|
+
if (typeof window !== "undefined" && settings.fallback_401_url) {
|
|
82
|
+
window.location.href = settings.fallback_401_url;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return Promise.reject(error);
|
|
86
|
+
});
|
|
87
|
+
return instance;
|
|
88
|
+
}
|
package/dist/hooks/useAuth.d.ts
CHANGED
package/dist/hooks/useAuth.js
CHANGED
|
@@ -8,6 +8,8 @@ export function useAuth() {
|
|
|
8
8
|
throw new Error("useAuth must be used within an AuthContext");
|
|
9
9
|
// -------------------------------------------------- data
|
|
10
10
|
const { permits: permitsData, ...user } = ctx.userData;
|
|
11
|
+
const access_token = ctx.access_token;
|
|
12
|
+
const refresh_token = ctx.refresh_token;
|
|
11
13
|
const authera_props = ctx.authera_props;
|
|
12
14
|
const prm = (permitsData || []);
|
|
13
15
|
// -------------------------------------------------- funtions
|
|
@@ -68,6 +70,8 @@ export function useAuth() {
|
|
|
68
70
|
setAccessToken,
|
|
69
71
|
setRefreshToken,
|
|
70
72
|
logout,
|
|
73
|
+
access_token,
|
|
74
|
+
refresh_token,
|
|
71
75
|
...authera_props,
|
|
72
76
|
};
|
|
73
77
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,10 @@ export default function AuthHook<T extends string>(props: AuthHookSettings<T>):
|
|
|
25
25
|
setAccessToken: (token: string) => void;
|
|
26
26
|
setRefreshToken: (token: string) => void;
|
|
27
27
|
logout: () => void;
|
|
28
|
+
access_token: string | null | undefined;
|
|
29
|
+
refresh_token: string | null | undefined;
|
|
28
30
|
};
|
|
31
|
+
axios: import("axios").AxiosInstance;
|
|
29
32
|
LoginScenario: (prop: {
|
|
30
33
|
submitButtonClassName?: string;
|
|
31
34
|
submitButtonText?: string;
|
package/dist/index.js
CHANGED
|
@@ -4,15 +4,18 @@ import { name2storage } from "./helper/storage";
|
|
|
4
4
|
import AuthProvider from "./web";
|
|
5
5
|
import { useAuth } from "./hooks/useAuth";
|
|
6
6
|
import LoginForm from "./web/login";
|
|
7
|
+
import createAxios from "./helper/axios";
|
|
7
8
|
export default function AuthHook(props) {
|
|
8
9
|
// set storage functions
|
|
9
10
|
let storage = props.storage;
|
|
10
11
|
if (typeof storage === "string")
|
|
11
12
|
storage = name2storage(storage);
|
|
12
13
|
// create backend data
|
|
14
|
+
const axios = createAxios(storage, props);
|
|
13
15
|
return {
|
|
14
16
|
createAuthProvider: (children) => (_jsx(AuthProvider, { storage: storage, authera_props: props, children: children })),
|
|
15
17
|
useAuth: () => useAuth(),
|
|
18
|
+
axios,
|
|
16
19
|
LoginScenario: (prop) => (_jsx(LoginForm, { on_after_login: props.on_after_login, on_after_step: props.on_after_step, backendUrl: props.backendUrl, submitButtonClassName: prop.submitButtonClassName, submitButtonText: prop.submitButtonText })),
|
|
17
20
|
};
|
|
18
21
|
}
|
package/dist/web/login.js
CHANGED
|
@@ -16,7 +16,7 @@ export default function LoginForm({ on_after_login, on_after_step, backendUrl, s
|
|
|
16
16
|
const { handleSubmit, control } = useForm();
|
|
17
17
|
const { setUserData, setPermits } = useAuth();
|
|
18
18
|
const request = axios.create({
|
|
19
|
-
baseURL: backendUrl,
|
|
19
|
+
baseURL: backendUrl + "/auth",
|
|
20
20
|
});
|
|
21
21
|
// fetch data from steps
|
|
22
22
|
useEffect(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "authera",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
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
|
+
}
|