|
@@ -1,4 +1,3 @@
|
|
|
-import { useState } from 'react'
|
|
|
|
|
import { z } from 'zod'
|
|
import { z } from 'zod'
|
|
|
import { useForm } from 'react-hook-form'
|
|
import { useForm } from 'react-hook-form'
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
|
@@ -6,7 +5,7 @@ import { useNavigate } from '@tanstack/react-router'
|
|
|
import { Loader2, LogIn } from 'lucide-react'
|
|
import { Loader2, LogIn } from 'lucide-react'
|
|
|
import { toast } from 'sonner'
|
|
import { toast } from 'sonner'
|
|
|
|
|
|
|
|
-import { useAuthStore } from '@/stores/auth-store'
|
|
|
|
|
|
|
+import { useLogin } from '@/hooks/auth/useAuth'
|
|
|
import { cn } from '@/lib/utils'
|
|
import { cn } from '@/lib/utils'
|
|
|
import { Button } from '@/components/ui/button'
|
|
import { Button } from '@/components/ui/button'
|
|
|
import {
|
|
import {
|
|
@@ -40,9 +39,8 @@ export function UserAuthForm({
|
|
|
redirectTo,
|
|
redirectTo,
|
|
|
...props
|
|
...props
|
|
|
}: UserAuthFormProps) {
|
|
}: UserAuthFormProps) {
|
|
|
- const [isLoading, setIsLoading] = useState(false)
|
|
|
|
|
const navigate = useNavigate()
|
|
const navigate = useNavigate()
|
|
|
- const login = useAuthStore((s) => s.login)
|
|
|
|
|
|
|
+ const { mutateAsync: login, isPending } = useLogin()
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
resolver: zodResolver(formSchema),
|
|
resolver: zodResolver(formSchema),
|
|
@@ -53,16 +51,17 @@ export function UserAuthForm({
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const onSubmit = async (data: z.infer<typeof formSchema>) => {
|
|
const onSubmit = async (data: z.infer<typeof formSchema>) => {
|
|
|
- setIsLoading(true)
|
|
|
|
|
try {
|
|
try {
|
|
|
await login(data)
|
|
await login(data)
|
|
|
|
|
|
|
|
toast.success('ورود با موفقیت انجام شد')
|
|
toast.success('ورود با موفقیت انجام شد')
|
|
|
- navigate({ to: redirectTo || '/', replace: true })
|
|
|
|
|
|
|
+
|
|
|
|
|
+ navigate({
|
|
|
|
|
+ to: redirectTo || '/',
|
|
|
|
|
+ replace: true,
|
|
|
|
|
+ })
|
|
|
} catch {
|
|
} catch {
|
|
|
toast.error('نام کاربری یا رمز عبور اشتباه است')
|
|
toast.error('نام کاربری یا رمز عبور اشتباه است')
|
|
|
- } finally {
|
|
|
|
|
- setIsLoading(false)
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -79,6 +78,7 @@ export function UserAuthForm({
|
|
|
render={({ field }) => (
|
|
render={({ field }) => (
|
|
|
<FormItem>
|
|
<FormItem>
|
|
|
<FormLabel>نام کاربری</FormLabel>
|
|
<FormLabel>نام کاربری</FormLabel>
|
|
|
|
|
+
|
|
|
<FormControl>
|
|
<FormControl>
|
|
|
<Input
|
|
<Input
|
|
|
placeholder='نام کاربری یا شماره موبایل'
|
|
placeholder='نام کاربری یا شماره موبایل'
|
|
@@ -86,6 +86,7 @@ export function UserAuthForm({
|
|
|
{...field}
|
|
{...field}
|
|
|
/>
|
|
/>
|
|
|
</FormControl>
|
|
</FormControl>
|
|
|
|
|
+
|
|
|
<FormMessage />
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
</FormItem>
|
|
|
)}
|
|
)}
|
|
@@ -97,6 +98,7 @@ export function UserAuthForm({
|
|
|
render={({ field }) => (
|
|
render={({ field }) => (
|
|
|
<FormItem>
|
|
<FormItem>
|
|
|
<FormLabel>رمز عبور</FormLabel>
|
|
<FormLabel>رمز عبور</FormLabel>
|
|
|
|
|
+
|
|
|
<FormControl>
|
|
<FormControl>
|
|
|
<PasswordInput
|
|
<PasswordInput
|
|
|
placeholder='********'
|
|
placeholder='********'
|
|
@@ -104,13 +106,14 @@ export function UserAuthForm({
|
|
|
{...field}
|
|
{...field}
|
|
|
/>
|
|
/>
|
|
|
</FormControl>
|
|
</FormControl>
|
|
|
|
|
+
|
|
|
<FormMessage />
|
|
<FormMessage />
|
|
|
</FormItem>
|
|
</FormItem>
|
|
|
)}
|
|
)}
|
|
|
/>
|
|
/>
|
|
|
|
|
|
|
|
- <Button className='mt-2' disabled={isLoading}>
|
|
|
|
|
- {isLoading ? (
|
|
|
|
|
|
|
+ <Button className='mt-2' disabled={isPending}>
|
|
|
|
|
+ {isPending ? (
|
|
|
<Loader2 className='mr-2 h-4 w-4 animate-spin' />
|
|
<Loader2 className='mr-2 h-4 w-4 animate-spin' />
|
|
|
) : (
|
|
) : (
|
|
|
<LogIn className='mr-2 h-4 w-4' />
|
|
<LogIn className='mr-2 h-4 w-4' />
|