authera 2.2.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.js +7 -24
- package/dist/hooks/useAuth.d.ts +2 -0
- package/dist/hooks/useAuth.js +4 -0
- package/dist/index.d.ts +2 -0
- package/package.json +1 -1
package/dist/helper/axios.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import axios from "axios";
|
|
3
|
+
import { useAuth } from "../hooks/useAuth";
|
|
3
4
|
/**
|
|
4
5
|
* Create a preconfigured Axios instance that:
|
|
5
6
|
* - Attaches Authorization header from storage on each request
|
|
@@ -7,34 +8,17 @@ import axios from "axios";
|
|
|
7
8
|
* - If refresh fails, redirects to fallback_401_url
|
|
8
9
|
*/
|
|
9
10
|
export default function createAxios(storage, settings) {
|
|
11
|
+
const { setAccessToken, setRefreshToken, access_token, refresh_token } = useAuth();
|
|
10
12
|
const instance = axios.create({
|
|
11
13
|
baseURL: settings.backendUrl,
|
|
12
14
|
});
|
|
13
15
|
// Single-flight refresh across concurrent 401s
|
|
14
16
|
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
|
-
};
|
|
33
17
|
const writeTokens = (access, refresh) => {
|
|
34
18
|
if (access)
|
|
35
|
-
|
|
19
|
+
setAccessToken(access);
|
|
36
20
|
if (refresh)
|
|
37
|
-
|
|
21
|
+
setRefreshToken(refresh);
|
|
38
22
|
};
|
|
39
23
|
const resolveAccessFromResponse = (data) => {
|
|
40
24
|
return data?.access_token ?? data?.accessToken ?? data?.token ?? null;
|
|
@@ -43,7 +27,7 @@ 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 =
|
|
30
|
+
const token = refresh_token;
|
|
47
31
|
if (!token)
|
|
48
32
|
return null;
|
|
49
33
|
try {
|
|
@@ -70,11 +54,10 @@ export default function createAxios(storage, settings) {
|
|
|
70
54
|
};
|
|
71
55
|
// Attach Authorization header on each request
|
|
72
56
|
instance.interceptors.request.use((config) => {
|
|
73
|
-
|
|
74
|
-
if (access) {
|
|
57
|
+
if (access_token) {
|
|
75
58
|
config.headers = {
|
|
76
59
|
...config.headers,
|
|
77
|
-
Authorization: `Bearer ${
|
|
60
|
+
Authorization: `Bearer ${access_token}`,
|
|
78
61
|
};
|
|
79
62
|
}
|
|
80
63
|
return config;
|
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,6 +25,8 @@ 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
|
};
|
|
29
31
|
axios: import("axios").AxiosInstance;
|
|
30
32
|
LoginScenario: (prop: {
|