authera 1.1.2 → 1.2.0
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/context.d.ts +2 -1
- package/dist/hooks/useAuth.d.ts +6 -1
- package/dist/hooks/useAuth.js +1 -2
- package/dist/index.d.ts +7 -1
- package/dist/index.js +2 -2
- package/dist/web/index.d.ts +3 -2
- package/dist/web/index.js +2 -2
- package/dist/web/login.d.ts +1 -5
- package/dist/web/login.js +30 -13
- package/package.json +1 -1
package/dist/helper/context.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import { type AuthHookSettings } from "..";
|
|
2
3
|
export type userBaseData<T extends string> = {
|
|
3
4
|
id: string;
|
|
4
5
|
username: string;
|
|
5
6
|
permits: T[];
|
|
6
7
|
};
|
|
7
8
|
export interface AuthContextValueType<T extends string> {
|
|
8
|
-
|
|
9
|
+
authera_props: AuthHookSettings<T>;
|
|
9
10
|
userData: userBaseData<T>;
|
|
10
11
|
setUserData: (value: userBaseData<T>) => void;
|
|
11
12
|
}
|
package/dist/hooks/useAuth.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { userBaseData } from "../helper/context";
|
|
2
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
|
+
go_after_login_url: string;
|
|
3
9
|
permits: T[];
|
|
4
10
|
isPermitted: (perm: T) => boolean;
|
|
5
11
|
on: (perm: T, callback: () => void, fallback?: () => void) => void;
|
|
@@ -7,6 +13,5 @@ export declare function useAuth<T extends string>(): {
|
|
|
7
13
|
addPermits: (permits: T[]) => void;
|
|
8
14
|
removePermits: (permits: T[]) => void;
|
|
9
15
|
setUserData: (userData: userBaseData<T>) => void;
|
|
10
|
-
fallback_401_url: string;
|
|
11
16
|
isPermittedAll: (perms: T[]) => boolean;
|
|
12
17
|
};
|
package/dist/hooks/useAuth.js
CHANGED
|
@@ -8,7 +8,6 @@ export function useAuth() {
|
|
|
8
8
|
// -------------------------------------------------- data
|
|
9
9
|
const { permits: permitsData } = ctx.userData;
|
|
10
10
|
const prm = (permitsData || []);
|
|
11
|
-
const fallback_401_url = ctx.fallback_401_url;
|
|
12
11
|
// -------------------------------------------------- funtions
|
|
13
12
|
const isPermitted = (perm) => {
|
|
14
13
|
return prm.includes(perm);
|
|
@@ -50,7 +49,7 @@ export function useAuth() {
|
|
|
50
49
|
addPermits,
|
|
51
50
|
removePermits,
|
|
52
51
|
setUserData,
|
|
53
|
-
fallback_401_url,
|
|
54
52
|
isPermittedAll,
|
|
53
|
+
...ctx.authera_props,
|
|
55
54
|
};
|
|
56
55
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,10 +5,17 @@ export interface AuthHookSettings<T extends string> {
|
|
|
5
5
|
tokenType: "jwt";
|
|
6
6
|
refreshStrategy: "silent";
|
|
7
7
|
fallback_401_url: string;
|
|
8
|
+
go_after_login_url: string;
|
|
8
9
|
}
|
|
9
10
|
export default function AuthHook<T extends string>(props: AuthHookSettings<T>): {
|
|
10
11
|
createAuthProvider: (children: React.ReactNode) => React.ReactNode;
|
|
11
12
|
useAuth: () => {
|
|
13
|
+
backendUrl: string;
|
|
14
|
+
storage: storagesNames | customeFunc;
|
|
15
|
+
tokenType: "jwt";
|
|
16
|
+
refreshStrategy: "silent";
|
|
17
|
+
fallback_401_url: string;
|
|
18
|
+
go_after_login_url: string;
|
|
12
19
|
permits: T[];
|
|
13
20
|
isPermitted: (perm: T) => boolean;
|
|
14
21
|
on: (perm: T, callback: () => void, fallback?: (() => void) | undefined) => void;
|
|
@@ -16,7 +23,6 @@ export default function AuthHook<T extends string>(props: AuthHookSettings<T>):
|
|
|
16
23
|
addPermits: (permits: T[]) => void;
|
|
17
24
|
removePermits: (permits: T[]) => void;
|
|
18
25
|
setUserData: (userData: import("./helper/context").userBaseData<T>) => void;
|
|
19
|
-
fallback_401_url: string;
|
|
20
26
|
isPermittedAll: (perms: T[]) => boolean;
|
|
21
27
|
};
|
|
22
28
|
LoginScenario: () => import("react/jsx-runtime").JSX.Element;
|
package/dist/index.js
CHANGED
|
@@ -11,9 +11,9 @@ export default function AuthHook(props) {
|
|
|
11
11
|
storage = name2storage(storage);
|
|
12
12
|
// create backend data
|
|
13
13
|
return {
|
|
14
|
-
createAuthProvider: (children) => (_jsx(AuthProvider, { storage: storage,
|
|
14
|
+
createAuthProvider: (children) => (_jsx(AuthProvider, { storage: storage, authera_props: props, children: children })),
|
|
15
15
|
useAuth: () => useAuth(),
|
|
16
|
-
LoginScenario: () => _jsx(LoginForm, {
|
|
16
|
+
LoginScenario: () => _jsx(LoginForm, {}),
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
19
|
export { default as AuthGuard } from "./web/guard";
|
package/dist/web/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import type { customeFunc as storageFunc } from "../helper/storage";
|
|
3
|
+
import { type AuthHookSettings } from "..";
|
|
3
4
|
interface AuthProviderProps<T extends string> {
|
|
4
5
|
children: React.ReactNode;
|
|
5
6
|
storage: storageFunc;
|
|
6
|
-
|
|
7
|
+
authera_props: AuthHookSettings<T>;
|
|
7
8
|
}
|
|
8
|
-
export default function AuthProvider<T extends string>({ children, storage,
|
|
9
|
+
export default function AuthProvider<T extends string>({ children, storage, authera_props, }: AuthProviderProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
9
10
|
export {};
|
package/dist/web/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { AuthContext } from "../helper/context";
|
|
4
|
-
export default function AuthProvider({ children, storage,
|
|
4
|
+
export default function AuthProvider({ children, storage, authera_props, }) {
|
|
5
5
|
/**
|
|
6
6
|
* Context Provider
|
|
7
7
|
*/
|
|
@@ -10,5 +10,5 @@ export default function AuthProvider({ children, storage, fallback_401_url, }) {
|
|
|
10
10
|
const setUserData = (value) => {
|
|
11
11
|
storage.set("userData", value);
|
|
12
12
|
};
|
|
13
|
-
return (_jsx(Provider, { value: { userData, setUserData,
|
|
13
|
+
return (_jsx(Provider, { value: { userData, setUserData, authera_props }, children: children }));
|
|
14
14
|
}
|
package/dist/web/login.d.ts
CHANGED
package/dist/web/login.js
CHANGED
|
@@ -4,32 +4,49 @@ import { convertFromSchema } from "minimal-form";
|
|
|
4
4
|
import EasyForm from "minimal-form";
|
|
5
5
|
import { useEffect, useState } from "react";
|
|
6
6
|
import { useForm } from "react-hook-form";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const [
|
|
7
|
+
import { useAuth } from "../hooks/useAuth";
|
|
8
|
+
export default function LoginForm() {
|
|
9
|
+
const [steps, stepsHnadler] = useState();
|
|
10
|
+
const [activeStep, activeStepHandler] = useState();
|
|
10
11
|
const [loading, loadingHandler] = useState(true);
|
|
11
12
|
const { handleSubmit, control } = useForm();
|
|
13
|
+
const { go_after_login_url, backendUrl } = useAuth();
|
|
12
14
|
const request = axios.create({
|
|
13
|
-
baseURL,
|
|
15
|
+
baseURL: backendUrl,
|
|
14
16
|
});
|
|
15
17
|
// fetch data from steps
|
|
16
18
|
useEffect(() => {
|
|
17
19
|
async function fetchSteps() {
|
|
18
|
-
const response = await request.get("/options");
|
|
19
|
-
const data = Object.entries(response.data).map(([opt, schema]) =>
|
|
20
|
-
opt,
|
|
21
|
-
convertFromSchema(schema),
|
|
22
|
-
|
|
23
|
-
activeStepHandler(data[0]
|
|
24
|
-
stepsHnadler(
|
|
20
|
+
const response = await request.get("validate/options");
|
|
21
|
+
const data = Object.entries(response.data).map(([opt, schema]) => ({
|
|
22
|
+
name: opt,
|
|
23
|
+
structure: convertFromSchema(schema),
|
|
24
|
+
}));
|
|
25
|
+
activeStepHandler(data[0]);
|
|
26
|
+
stepsHnadler(data);
|
|
25
27
|
loadingHandler(false);
|
|
26
28
|
}
|
|
27
29
|
fetchSteps();
|
|
28
30
|
}, []);
|
|
29
|
-
const onSubmit = (data) => {
|
|
31
|
+
const onSubmit = async (data) => {
|
|
32
|
+
const response = await request.post(activeStep.name, data);
|
|
33
|
+
// if response say go next step
|
|
34
|
+
if (response.status === 202) {
|
|
35
|
+
const nextIndex = (steps?.findIndex((d) => d.name === activeStep?.name) || 0) + 1;
|
|
36
|
+
activeStepHandler(steps?.[nextIndex]);
|
|
37
|
+
}
|
|
38
|
+
// if response say authenticate is finished
|
|
39
|
+
else if (response.status === 200) {
|
|
40
|
+
window.location.href = go_after_login_url;
|
|
41
|
+
}
|
|
42
|
+
// if response say have a wrong in this request
|
|
43
|
+
else if (response.status === 400) {
|
|
44
|
+
activeStepHandler(steps?.[0]);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
30
47
|
// loading check
|
|
31
48
|
if (loading)
|
|
32
49
|
return _jsx("p", { children: "loading ..." });
|
|
33
50
|
// show forms from steps
|
|
34
|
-
return (_jsxs("form", { onSubmit: handleSubmit(onSubmit), children: [_jsx(EasyForm, { control: control, structure:
|
|
51
|
+
return (_jsxs("form", { onSubmit: handleSubmit(onSubmit), children: [_jsx(EasyForm, { control: control, structure: activeStep.structure }), _jsx("button", { type: "submit", children: "Submit" })] }));
|
|
35
52
|
}
|