complete login
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { Button } from "@headlessui/react"
|
||||
import clsx from "clsx"
|
||||
import { FC } from "react"
|
||||
|
||||
type Props = {
|
||||
title: string
|
||||
type?: "reset" | "button" | "submit" | undefined,
|
||||
pending?: boolean,
|
||||
theme?: "primary" | "secondary" | "glass" | "light"
|
||||
}
|
||||
|
||||
export const ButtonComponent: FC<Props> = ({ title, type = "button", pending = false, theme = "primary" }) => {
|
||||
return (
|
||||
<Button type={type} className={clsx("flex flex-row gap-3 items-center justify-center", {
|
||||
"bg-primary-color text-white": theme === "primary",
|
||||
"bg-transparent text-primary-color": theme === "glass",
|
||||
"bg-dark-blue text-white": theme === "secondary",
|
||||
"bg-sky-blue text-white": theme === "light",
|
||||
})}>
|
||||
<p className="text-base font-medium text-white">{title}</p>
|
||||
{pending && <svg aria-hidden="true" className="size-5 text-white animate-spin fill-primary-color" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
|
||||
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
|
||||
</svg>}
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
@@ -1,13 +1,22 @@
|
||||
import { Link } from "react-router-dom"
|
||||
import { Link, useNavigate } from "react-router-dom"
|
||||
import { Input } from "../common/input"
|
||||
import { Button } from "@headlessui/react"
|
||||
import { Controller, SubmitHandler, useForm } from "react-hook-form"
|
||||
import { LoginFormInterface } from "../../types"
|
||||
import { loginSchema } from "../../schema"
|
||||
import { ErrorComponent } from "../common/error"
|
||||
import { yupResolver } from '@hookform/resolvers/yup'
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { loginMutation } from "../../services/api/auth"
|
||||
import { useAuthStore } from "../../store/auth-token"
|
||||
import { ButtonComponent } from "../common/button"
|
||||
import toast from "react-hot-toast"
|
||||
|
||||
export const LoginForm = () => {
|
||||
const navigate = useNavigate()
|
||||
const { setToken: setLoginToken } = useAuthStore()
|
||||
const { mutate, isPending } = useMutation({
|
||||
mutationFn: loginMutation
|
||||
})
|
||||
const {
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
@@ -15,9 +24,22 @@ export const LoginForm = () => {
|
||||
} = useForm<LoginFormInterface>({
|
||||
resolver: yupResolver(loginSchema)
|
||||
})
|
||||
const handleLoginSubmit: SubmitHandler<any> = async (data) => {
|
||||
console.log("data=>", data)
|
||||
const handleLoginSubmit: SubmitHandler<LoginFormInterface> = async (data) => {
|
||||
mutate({
|
||||
...data,
|
||||
remember_me: true
|
||||
}, {
|
||||
onSuccess: (data: any) => {
|
||||
setLoginToken(data?.data?.token)
|
||||
toast.success("با موفقیت وارد شدین")
|
||||
navigate("/")
|
||||
},
|
||||
onError: () => {
|
||||
toast.error("نام کاربری یا رمز عبور اشتباه است")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit(handleLoginSubmit)}
|
||||
className="w-full sm:w-fit sm:min-w-[500px] xl:w-1/2 xl:p-0 p-10 flex flex-col bg-auth-form justify-center items-center gap-y-[42px] rounded-3xl">
|
||||
@@ -33,10 +55,10 @@ export const LoginForm = () => {
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-y-[25px] mt-9 mb-[42px]">
|
||||
<Controller control={control} name="phoneNumber" render={({ field }) => (
|
||||
<Controller control={control} name="username" render={({ field }) => (
|
||||
<div>
|
||||
<Input type="number" placeholder="شماره تماس" field={field} />
|
||||
<ErrorComponent show={errors.phoneNumber} message={errors.phoneNumber?.message} />
|
||||
<Input type="number" placeholder="نام کاربری" field={field} />
|
||||
<ErrorComponent show={errors.username} message={errors.username?.message} />
|
||||
</div>
|
||||
)} />
|
||||
<Controller control={control} name="password" render={({ field }) => (
|
||||
@@ -48,7 +70,7 @@ export const LoginForm = () => {
|
||||
<Link to="/auth/forgot-password"><p className="font-medium text-sm text-secondary-text-color cursor-pointer">فراموشی رمز عبور</p></Link>
|
||||
</div>
|
||||
<div className="flex flex-col gap-y-[27px] items-center">
|
||||
<Button type="submit">ثبت</Button>
|
||||
<ButtonComponent type="submit" title="ثبت" pending={isPending} />
|
||||
<Link to="/auth/login-with-code"><p className="font-medium text-base text-primary-color cursor-pointer">ورود با رمز یکبار مصرف</p></Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+4
-2
@@ -4,10 +4,9 @@ import { RouterProvider } from 'react-router-dom';
|
||||
import { router } from './router';
|
||||
import './index.css'
|
||||
import "iranianbanklogos/dist/ibl.css";
|
||||
import { Toaster } from 'react-hot-toast';
|
||||
import { RQProvider } from './rq-provider';
|
||||
|
||||
|
||||
|
||||
const root = ReactDOM.createRoot(
|
||||
document.getElementById('root') as HTMLElement
|
||||
);
|
||||
@@ -16,6 +15,9 @@ root.render(
|
||||
<React.StrictMode>
|
||||
<RQProvider>
|
||||
<RouterProvider router={router} />
|
||||
<Toaster position='top-center' toastOptions={{
|
||||
duration: 4000,
|
||||
}} />
|
||||
</RQProvider>
|
||||
</React.StrictMode>
|
||||
);
|
||||
|
||||
+5
-5
@@ -1,11 +1,11 @@
|
||||
import * as yup from 'yup'
|
||||
|
||||
export const loginSchema = yup.object({
|
||||
phoneNumber: yup.string()
|
||||
.required("وارد کردن شماره تماس الزامی میباشد")
|
||||
.min(11, "شماره تماس نمیتوند کمتر از 11 رقم باشد")
|
||||
.max(11, "شماره تماس نمیتواند بیشتر از 11 رقم باشد")
|
||||
.matches(/^[0-9]{11}$/, "شماره تماس نامعتبر"),
|
||||
username: yup.string()
|
||||
.required("وارد کردن نام کاربری الزامی میباشد")
|
||||
.min(10, "نام کاربری نمیتوند کمتر از 10 رقم باشد")
|
||||
.max(10, "نام کاربری نمیتواند بیشتر از 10 رقم باشد")
|
||||
.matches(/^[0-9]{10}$/, "شماره تماس نامعتبر"),
|
||||
password: yup.string()
|
||||
.required("وارد کردن رمزعبور الزامی میباشد")
|
||||
.min(8, "رمزعبور نمیتواند کمتر از 8 کاراکتر باشد")
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import { axiosInstance } from "../axios";
|
||||
|
||||
export const loginMutation = (payload: any) => axiosInstance.post<any>("/auth/gps/login", payload)
|
||||
@@ -1,7 +1,7 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
|
||||
const axiosParam: AxiosRequestConfig = {
|
||||
baseURL: "http://localhost:4000"
|
||||
baseURL: process.env.REACT_APP_API_Address
|
||||
}
|
||||
|
||||
export const axiosInstance = axios.create(axiosParam)
|
||||
@@ -0,0 +1,11 @@
|
||||
import create from 'zustand';
|
||||
|
||||
interface AuthState {
|
||||
token: string | null;
|
||||
setToken: (token: string | null) => void;
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>((set) => ({
|
||||
token: null,
|
||||
setToken: (token) => set({ token }),
|
||||
}));
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
import React from "react"
|
||||
|
||||
export interface LoginFormInterface {
|
||||
phoneNumber: string,
|
||||
username: string,
|
||||
password: string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user