Skip to content

Commit

Permalink
feat(auth-login): implement login action
Browse files Browse the repository at this point in the history
  • Loading branch information
ridhlab committed Dec 27, 2023
1 parent 076bb03 commit 51cd38a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/components/modules/Auth/Login/LoginCard.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useAuthContext } from "@/contexts/AuthContext";
import { setAuthCookie } from "@/helpers/cookie";
import { getMinCharMessage, getRequiredMessage } from "@/helpers/form";
import { prompNotification } from "@/helpers/notification";
import { hasNumberRegex } from "@/helpers/validation";
Expand All @@ -6,7 +8,7 @@ import { ILoginRequest } from "@/interfaces/requests/Auth";
import { Routes } from "@/routes/routes";
import { useLoginMutation } from "@/services/mutation/Auth";
import { Button, Card, Input, Row, Space, Typography, Form } from "antd";
import { Link } from "react-router-dom";
import { Link, useNavigate } from "react-router-dom";
import * as yup from "yup";

const schema: yup.ObjectSchema<ILoginRequest> = yup.object({
Expand All @@ -26,10 +28,25 @@ const schema: yup.ObjectSchema<ILoginRequest> = yup.object({
export const LoginCard = () => {
const { form, yupSync } = useFormUtility<ILoginRequest>({ schema });
const watch = Form.useWatch([], form);
const { afterLoginRegister } = useAuthContext();

const navigate = useNavigate();

const mutation = useLoginMutation({
onError: (error) => {
prompNotification({ method: "error", message: error as string });
prompNotification({ method: "error", message: error.message });
},
onSuccess: (response) => {
const {
data: { token, user },
} = response;
setAuthCookie(token);
afterLoginRegister(user);
navigate(Routes.Dashboard, { replace: true });
prompNotification({
method: "success",
message: "Login successfully",
});
},
});

Expand Down

0 comments on commit 51cd38a

Please sign in to comment.