|
|
@ -1,4 +1,4 @@ |
|
|
|
"use client" |
|
|
|
"use client"; |
|
|
|
import React from "react"; |
|
|
|
import styles from "./loginPage.module.scss"; |
|
|
|
import Box from "@mui/material/Box"; |
|
|
@ -8,12 +8,12 @@ import { UTILITY_CONSTANT } from "@/utilities/utilityConstant"; |
|
|
|
import Link from "next/link"; |
|
|
|
import CustomizedInputsStyled from "@/ui/CustomizedInputsStyled"; |
|
|
|
import CustomizedButtons from "@/ui/customizedButtons"; |
|
|
|
import { useForm } from 'react-hook-form'; |
|
|
|
import { z } from 'zod'; |
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'; |
|
|
|
import { useState } from 'react'; |
|
|
|
import { useForm } from "react-hook-form"; |
|
|
|
import { z } from "zod"; |
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"; |
|
|
|
import { useState } from "react"; |
|
|
|
import { loginApi } from "@/services/api/loginApi"; |
|
|
|
import { FormControl } from '@mui/material'; |
|
|
|
import { FormControl } from "@mui/material"; |
|
|
|
import CustomTextField from "@/ui/CustomTextField"; |
|
|
|
import { useRouter } from "next/navigation"; |
|
|
|
import { useDispatch, useSelector } from "react-redux"; |
|
|
@ -28,22 +28,30 @@ const loginSchema = z.object({ |
|
|
|
type LoginFormValues = z.infer<typeof loginSchema>; |
|
|
|
|
|
|
|
export default function LoginPage() { |
|
|
|
const [loader, setLoader] = useState(false); |
|
|
|
const router = useRouter() |
|
|
|
const { register, handleSubmit, formState: { errors }, control } = useForm<LoginFormValues>({ |
|
|
|
resolver: zodResolver(loginSchema), |
|
|
|
}); |
|
|
|
const [error, setError] = useState(''); |
|
|
|
const [error, setError] = useState(""); |
|
|
|
const dispatch = useDispatch(); |
|
|
|
const onSubmit = async (data: LoginFormValues) => { |
|
|
|
try { |
|
|
|
setLoader(true) |
|
|
|
const response = await loginApi(data); |
|
|
|
localStorage.setItem('token', response.token); |
|
|
|
localStorage.setItem('refreshToken', response.refreshToken); |
|
|
|
dispatch(setAuthTokens({ token: response.token, refreshToken: response.refreshToken })); |
|
|
|
localStorage.setItem("token", response.token); |
|
|
|
localStorage.setItem("refreshToken", response.refreshToken); |
|
|
|
dispatch( |
|
|
|
setAuthTokens({ |
|
|
|
token: response.token, |
|
|
|
refreshToken: response.refreshToken, |
|
|
|
}) |
|
|
|
); |
|
|
|
dispatch(setUserDetails(response)); |
|
|
|
|
|
|
|
setLoader(false) |
|
|
|
router.push('/home'); |
|
|
|
} catch (err) { |
|
|
|
setLoader(false) |
|
|
|
setError('Invalid credentials'); |
|
|
|
} |
|
|
|
}; |
|
|
@ -107,7 +115,7 @@ export default function LoginPage() { |
|
|
|
/> |
|
|
|
</Box> |
|
|
|
<Box width={"100%"} marginBlockStart={"20px"}> |
|
|
|
<CustomizedButtons btnType="submit" label={"Sign In"} /> |
|
|
|
<CustomizedButtons btnType="submit" label={!loader ? "Login" : "Login..."} /> |
|
|
|
</Box> |
|
|
|
</form> |
|
|
|
<Box |
|
|
|