authera 1.1.2 → 1.1.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.
Files changed (2) hide show
  1. package/dist/web/login.js +27 -10
  2. package/package.json +1 -1
package/dist/web/login.js CHANGED
@@ -4,11 +4,13 @@ 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
+ import { useAuth } from "../hooks/useAuth";
7
8
  export default function LoginForm({ baseURL }) {
8
- const [steps, stepsHnadler] = useState({});
9
- const [activeStep, activeStepHandler] = useState("");
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 { fallback_401_url } = useAuth();
12
14
  const request = axios.create({
13
15
  baseURL,
14
16
  });
@@ -16,20 +18,35 @@ export default function LoginForm({ baseURL }) {
16
18
  useEffect(() => {
17
19
  async function fetchSteps() {
18
20
  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][0]);
24
- stepsHnadler(Object.fromEntries(data));
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 = fallback_401_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: steps[activeStep] }), _jsx("button", { type: "submit", children: "Submit" })] }));
51
+ return (_jsxs("form", { onSubmit: handleSubmit(onSubmit), children: [_jsx(EasyForm, { control: control, structure: activeStep.structure }), _jsx("button", { type: "submit", children: "Submit" })] }));
35
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authera",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "this project is a simple auth hook for react",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",