Guest User

Untitled

a guest
Mar 28th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | Source Code | 0 0
  1. 'use client'
  2.  
  3. import Image from "next/image";
  4. import Link from "next/link";
  5. import SubmitBtn from "./SubmitBtn";
  6.  
  7. import { FormEvent, useState } from "react";
  8. import { useRouter } from "next/navigation";
  9. import { Errors } from "@/types/auth";
  10.  
  11. const BE_API = "http://localhost:8000"
  12.  
  13. export default function RegisterForm() {
  14.  
  15. const [errorMsg, setErrorMsg] = useState<Errors>({});
  16. const router = useRouter();
  17.  
  18. const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
  19. e.preventDefault();
  20.  
  21. const formData = new FormData(e.currentTarget);
  22.  
  23. const email = formData.get("email");
  24. const password = formData.get("password");
  25. const password2 = formData.get("re-password");
  26.  
  27. try {
  28. const res = await fetch(`${BE_API}/api/register`, {
  29. method: "POST",
  30. headers: { 'Content-Type': 'application/json' },
  31. body: JSON.stringify({ email, password, password2 })
  32. })
  33.  
  34. const data = await res.json();
  35. setErrorMsg(data)
  36.  
  37.  
  38. if (!data.status) {
  39. return { error: data.message }
  40. }
  41. } catch (error) {
  42. return {
  43. message: "Please, enter a valid email and password."
  44. };
  45. }
  46.  
  47. router.push("/");
  48. }
  49.  
  50. return (
  51. <form onSubmit={handleSubmit} className="bg-white w-[500px] h-[500px] shadow-xl rounded-xl p-2 py-4 gap-1 flex flex-col items-center justify-around">
  52. <h1 className="text-xl font-bold uppercase py-3">Register</h1>
  53. <div className="flex flex-col">
  54. <input
  55. type="email"
  56. name="email"
  57. className="border-gray-300 border w-[430px] rounded-lg px-2 py-2"
  58. placeholder="Email"
  59. aria-describedby="email-error"
  60. />
  61. </div>
  62. <div id="email-error" aria-live="polite" aria-atomic="true" className="w-full pl-7">
  63. {errorMsg.errors?.email &&
  64. errorMsg.errors?.email.map((error: string) => (
  65. <p className="text-sm text-red-500 text-left" key={error}>
  66. {error}
  67. </p>
  68. ))}
  69. </div>
  70. <div className="flex flex-col">
  71. <input
  72. type="password"
  73. name="password"
  74. className="border-gray-300 border w-[430px] rounded-lg px-2 py-2"
  75. placeholder="Password"
  76. aria-describedby="password-error"
  77. />
  78. </div>
  79. <div id="password-error" aria-live="polite" aria-atomic="true" className="w-full pl-7">
  80. {errorMsg.errors?.password &&
  81. errorMsg.errors?.password.map((error: string) => (
  82. <p className="text-sm text-red-500 text-left" key={error}>
  83. {error}
  84. </p>
  85. ))}
  86. </div>
  87. <div className="flex flex-col">
  88. <input
  89. type="password"
  90. name="re-password"
  91. className="border-gray-300 border w-[430px] rounded-lg px-2 py-2"
  92. placeholder="Re-Password"
  93. aria-describedby="password2-error"
  94. />
  95. </div>
  96. <div id="password2-error" aria-live="polite" aria-atomic="true" className="w-full pl-7">
  97. {errorMsg.errors?.password2 &&
  98. errorMsg.errors?.password2.map((error: string) => (
  99. <p className="text-sm text-red-500 text-left" key={error}>
  100. {error}
  101. </p>
  102. ))}
  103. </div>
  104. <SubmitBtn operation="Register" />
  105. <div className="w-full text-sm text-black text-center">
  106. <h3 className="mt-1">
  107. Already have an account? <Link href={"/auth/login"} className="text-[#4070f4] font-bold hover:underline">Login here</Link>
  108. </h3>
  109. </div>
  110.  
  111. <hr className="w-[90%] my-2" />
  112.  
  113. <div className="my-2 flex flex-col gap-2 w-[90%]">
  114. <div className="cursor-pointer rounded-xl flex w-full h-[30px] items-center gap-5 border-gray-300 border px-2 py-5 hover:opacity-30 duration-200">
  115. <Image
  116. src="https://cdn1.iconfinder.com/data/icons/google-s-logo/150/Google_Icons-09-512.png"
  117. width={35}
  118. height={35}
  119. alt="google-logo"
  120. className="justify-start"
  121. />
  122. <button
  123. id="name"
  124. name="name"
  125. defaultValue=""
  126. className="py-2 rounded-md outline-none text-black w-[70%] text-center"
  127. >Register with Google</button>
  128. </div>
  129. </div>
  130. </form>
  131. )
  132. }
Advertisement
Add Comment
Please, Sign In to add comment