fix: registration redirect
Brijesh Wawdhane ops@brijesh.dev
Sat, 14 Dec 2024 03:24:01 +0530
2 files changed,
8 insertions(+),
5 deletions(-)
M
src/app/actions/auth.js
→
src/app/actions/auth.js
@@ -1,7 +1,5 @@
"use server"; -import { redirect } from "next/navigation"; - export async function login(formData) { try { const response = await fetch("http://localhost:8080/auth/login", {@@ -43,11 +41,13 @@ }),
}); if (!response.ok) { + const errorText = await response.text(); throw new Error("Registration failed"); } - redirect("/login"); + const data = await response.json(); + return { success: true, data }; } catch (error) { - return { success: false, error: "Registration failed" }; + return { success: false, error: "Registration failed " + error }; } }
M
src/components/auth/RegisterForm.js
→
src/components/auth/RegisterForm.js
@@ -1,6 +1,7 @@
"use client"; import { useRef, useState } from "react"; +import { useRouter } from "next/navigation"; import Input from "../shared/Input"; import Button from "../shared/Button"; import { register } from "@/app/actions/auth";@@ -10,6 +11,7 @@ const [error, setError] = useState("");
const [isSubmitting, setIsSubmitting] = useState(false); const formRef = useRef(); const passwordRef = useRef(); + const router = useRouter(); async function handleSubmit(formData) { setIsSubmitting(true);@@ -26,6 +28,8 @@ const result = await register(formData);
if (!result?.success && result?.error) { setError(result.error); + } else { + router.push("/login"); } setIsSubmitting(false);@@ -47,7 +51,6 @@ label="Email"
name="email" type="email" required - pattern="[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i" error={error} />