Guest User

Untitled

a guest
Sep 9th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. 'use client'
  2.  
  3. import { useState } from 'react';
  4. import { useForm } from "react-hook-form";
  5. import { zodResolver } from "@hookform/resolvers/zod";
  6. import { useRouter } from 'next/navigation'
  7. import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';
  8. import { Label } from '@/components/ui/label';
  9. import { Input } from '@/components/ui/input';
  10. import { Button } from '@/components/ui/button';
  11. import { loginSchema, LoginFormData } from "@/lib/schemas/loginSchema";
  12. import { loginAction } from '@/app/action';
  13. import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
  14.  
  15. const errorMessages = {
  16. user_invalid_credentials: "Invalid email or password. Please try again.",
  17. user_not_found: "No account found with this email. Please check and try again.",
  18. general_rate_limit_exceeded: "Too many login attempts. Please try again later.",
  19. user_blocked: "This account has been blocked. Please contact support.",
  20. user_password_reset_required: "Password reset required. Please check your email.",
  21. user_email_not_whitelisted: "This email is not authorized to access the system.",
  22. user_ip_not_whitelisted: "Access from your current location is not allowed.",
  23. default: "An unexpected error occurred. Please try again."
  24. };
  25.  
  26. export default function LoginForm() {
  27. const router = useRouter()
  28. const [serverError, setServerError] = useState<string | null>(null)
  29. const [isLoading, setIsLoading] = useState(false)
  30. const { register, handleSubmit, formState: { errors } } = useForm<LoginFormData>({
  31. resolver: zodResolver(loginSchema)
  32. })
  33.  
  34. const onSubmit = async (data: LoginFormData) => {
  35. setServerError(null)
  36. setIsLoading(true)
  37. try {
  38. const result = await loginAction(data.email, data.password);
  39. if (result && result.success) {
  40. console.log("Successfully logged in");
  41. router.push('/dashboard');
  42. } else {
  43. setServerError(result?.error || 'An unexpected error occurred')
  44. }
  45. } catch (error) {
  46. setServerError('An unexpected error occurred')
  47. console.error('Login error:', error)
  48. } finally {
  49. setIsLoading(false)
  50. }
  51. }
  52.  
  53. return (
  54. <Card className="w-[350px]">
  55. <CardHeader>
  56. <CardTitle>Login</CardTitle>
  57. <CardDescription>
  58. Enter your credentials to access your account.
  59. </CardDescription>
  60. </CardHeader>
  61. <CardContent>
  62. {serverError && <div className="text-red-500 mb-4">{serverError}</div>}
  63. <form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
  64. <div>
  65. <Label htmlFor="email">Email</Label>
  66. <Input id="email" {...register('email')} placeholder='Enter your email' />
  67. {errors.email && <p className="text-red-500">{errors.email.message}</p>}
  68. </div>
  69. <div>
  70. <Label htmlFor="password">Password</Label>
  71. <Input id="password" type="password" {...register('password')} placeholder='Enter your password' />
  72. {errors.password && <p className="text-red-500">{errors.password.message}</p>}
  73. </div>
  74. <Button
  75. className="w-full mt-6"
  76. type="submit"
  77. disabled={isLoading}
  78. >
  79. {isLoading ? (
  80. <>
  81. <LoadingSpinner />
  82. <span className="ml-2">Logging in...</span>
  83. </>
  84. ) : (
  85. 'Login'
  86. )}
  87. </Button>
  88. </form>
  89. </CardContent>
  90. <CardFooter className="flex justify-center">
  91. <a href="#" className="text-sm text-blue-600 hover:underline">
  92. Forgot password?
  93. </a>
  94. </CardFooter>
  95. </Card>
  96. )
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment