authera 1.2.8-test-19 → 1.2.8-test-20
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/hooks/useAuth.d.ts +22 -1
- package/dist/hooks/useAuth.js +68 -60
- package/dist/index.d.ts +22 -1
- package/package.json +1 -1
package/dist/hooks/useAuth.d.ts
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
import { userBaseData } from "../helper/context";
|
|
2
|
+
export declare function useAuth<T extends string>(): {
|
|
3
|
+
backendUrl: string;
|
|
4
|
+
storage: import("../helper/storage").storagesNames | import("../helper/storage").customeFunc;
|
|
5
|
+
tokenType: "jwt";
|
|
6
|
+
refreshStrategy: "silent";
|
|
7
|
+
fallback_401_url: string;
|
|
8
|
+
on_after_login?: (response_data: any) => void;
|
|
9
|
+
on_after_step?: (step_key: string) => void;
|
|
10
|
+
permits: string[];
|
|
11
|
+
isPermitted: (perm: T) => boolean;
|
|
12
|
+
on: (perm: T, callback: () => void, fallback?: () => void) => void;
|
|
13
|
+
setPermits: (permits: T[]) => void;
|
|
14
|
+
addPermits: (permits: string[]) => void;
|
|
15
|
+
removePermits: (permits: string[]) => void;
|
|
16
|
+
setUserData: (userData: userBaseData<T>) => void;
|
|
17
|
+
isPermittedAll: (perms: T[]) => boolean;
|
|
18
|
+
user: userBaseData<T> | null;
|
|
19
|
+
setAccessToken: (token: string) => void;
|
|
20
|
+
setRefreshToken: (token: string) => void;
|
|
21
|
+
logout: () => void;
|
|
22
|
+
};
|
package/dist/hooks/useAuth.js
CHANGED
|
@@ -1,71 +1,79 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { AuthContext, } from "../helper/context";
|
|
3
|
-
import React from "react";
|
|
3
|
+
import React, { useEffect, useState } from "react";
|
|
4
4
|
export function useAuth() {
|
|
5
|
+
const [prm, permitsHandler] = useState([]);
|
|
6
|
+
const [user, userHandler] = useState(null);
|
|
5
7
|
// -------------------------------------------------- context
|
|
6
8
|
const ctx = React.useContext(AuthContext);
|
|
7
9
|
if (!ctx)
|
|
8
10
|
throw new Error("useAuth must be used within an AuthContext");
|
|
9
11
|
// -------------------------------------------------- data
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const { permits: permitsData, ..._user } = ctx.userData;
|
|
14
|
+
permitsHandler(permitsData);
|
|
15
|
+
userHandler(_user);
|
|
16
|
+
}, []);
|
|
17
|
+
const authera_props = ctx.authera_props;
|
|
18
|
+
// -------------------------------------------------- funtions
|
|
19
|
+
const isPermitted = (perm) => {
|
|
20
|
+
return prm.includes(perm);
|
|
21
|
+
};
|
|
22
|
+
const isPermittedAll = (perms) => {
|
|
23
|
+
return perms.every((p) => prm.includes(p));
|
|
24
|
+
};
|
|
25
|
+
const on = (perm, callback, fallback) => {
|
|
26
|
+
if (isPermitted(perm))
|
|
27
|
+
callback();
|
|
28
|
+
else if (fallback)
|
|
29
|
+
fallback();
|
|
30
|
+
};
|
|
31
|
+
const setUserData = (userData) => {
|
|
32
|
+
console.log("userData", userData);
|
|
33
|
+
ctx.set("userData", userData);
|
|
34
|
+
};
|
|
35
|
+
const setPermits = (permits) => {
|
|
36
|
+
ctx.set("userData", { ...ctx.userData, permits });
|
|
37
|
+
};
|
|
38
|
+
const addPermits = (permits) => {
|
|
39
|
+
if (typeof permits !== "object")
|
|
40
|
+
return;
|
|
41
|
+
ctx.set("userData", {
|
|
42
|
+
...ctx.userData,
|
|
43
|
+
permits: [...prm.filter((p) => !permits.includes(p)), ...permits],
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
const removePermits = (permits) => {
|
|
47
|
+
ctx.set("userData", {
|
|
48
|
+
...ctx.userData,
|
|
49
|
+
permits: prm.filter((p) => !permits.includes(p)),
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const setAccessToken = (token) => {
|
|
53
|
+
ctx.set("access_token", token);
|
|
54
|
+
};
|
|
55
|
+
const setRefreshToken = (token) => {
|
|
56
|
+
ctx.set("refresh_token", token);
|
|
57
|
+
};
|
|
58
|
+
const logout = () => {
|
|
59
|
+
setUserData(null);
|
|
60
|
+
setPermits([]);
|
|
61
|
+
ctx.set("refresh_token", null);
|
|
62
|
+
ctx.set("access_token", null);
|
|
63
|
+
};
|
|
56
64
|
return {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
permits: prm,
|
|
66
|
+
isPermitted,
|
|
67
|
+
on,
|
|
68
|
+
setPermits,
|
|
69
|
+
addPermits,
|
|
70
|
+
removePermits,
|
|
71
|
+
setUserData,
|
|
72
|
+
isPermittedAll,
|
|
73
|
+
user,
|
|
74
|
+
setAccessToken,
|
|
75
|
+
setRefreshToken,
|
|
76
|
+
logout,
|
|
77
|
+
...authera_props,
|
|
70
78
|
};
|
|
71
79
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,28 @@
|
|
|
1
|
+
import { customeFunc, storagesNames } from "./helper/storage";
|
|
1
2
|
import { type AuthHookSettings } from "./helper/types";
|
|
2
3
|
export default function AuthHook<T extends string>(props: AuthHookSettings<T>): {
|
|
3
4
|
createAuthProvider: (children: React.ReactNode) => React.ReactNode;
|
|
4
|
-
useAuth: () => {
|
|
5
|
+
useAuth: () => {
|
|
6
|
+
backendUrl: string;
|
|
7
|
+
storage: storagesNames | customeFunc;
|
|
8
|
+
tokenType: "jwt";
|
|
9
|
+
refreshStrategy: "silent";
|
|
10
|
+
fallback_401_url: string;
|
|
11
|
+
on_after_login?: (response_data: any) => void;
|
|
12
|
+
on_after_step?: (step_key: string) => void;
|
|
13
|
+
permits: string[];
|
|
14
|
+
isPermitted: (perm: T) => boolean;
|
|
15
|
+
on: (perm: T, callback: () => void, fallback?: (() => void) | undefined) => void;
|
|
16
|
+
setPermits: (permits: T[]) => void;
|
|
17
|
+
addPermits: (permits: string[]) => void;
|
|
18
|
+
removePermits: (permits: string[]) => void;
|
|
19
|
+
setUserData: (userData: import("./helper/context").userBaseData<T>) => void;
|
|
20
|
+
isPermittedAll: (perms: T[]) => boolean;
|
|
21
|
+
user: import("./helper/context").userBaseData<T> | null;
|
|
22
|
+
setAccessToken: (token: string) => void;
|
|
23
|
+
setRefreshToken: (token: string) => void;
|
|
24
|
+
logout: () => void;
|
|
25
|
+
};
|
|
5
26
|
LoginScenario: () => import("react/jsx-runtime").JSX.Element;
|
|
6
27
|
};
|
|
7
28
|
export { default as AuthGuard } from "./web/guard";
|