Updated login and registration with email confirmation

This commit is contained in:
pascalyazid committed 2026-05-15 18:38:34 +02:00
1 parent 061bed2b59
commit 167baf9e82
5 files changed
+104 -22

No files matched your search

+8 -1
View File
@@ -6,7 +6,10 @@ import { loginAction, type LoginState } from "./actions";
const initialState: LoginState = null; const initialState: LoginState = null;
export default function LoginForm({ next }: { next?: string }) { export default function LoginForm({
next,
initialError,
}: Readonly<{ next?: string; initialError?: string }>) {
const [state, action, pending] = useActionState(loginAction, initialState); const [state, action, pending] = useActionState(loginAction, initialState);
return ( return (
@@ -47,6 +50,10 @@ export default function LoginForm({ next }: { next?: string }) {
</div> </div>
{state?.error ? ( {state?.error ? (
<p className="text-sm text-red-600">{state.error}</p> <p className="text-sm text-red-600">{state.error}</p>
) : initialError === "confirmation-failed" ? (
<p className="text-sm text-red-600">
Email confirmation failed. Please try registering again.
</p>
) : null} ) : null}
<button <button
type="submit" type="submit"
+13 -5
View File
@@ -1,14 +1,22 @@
import { redirect } from "next/navigation";
import { createClient } from "@/utils/supabase/server";
import LoginForm from "./login-form"; import LoginForm from "./login-form";
export default async function LoginPage({ export default async function LoginPage({
searchParams, searchParams,
}: { }: Readonly<{
searchParams: Promise<{ next?: string }>; searchParams: Promise<{ next?: string; error?: string }>;
}) { }>) {
const { next } = await searchParams; const supabase = await createClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (user) redirect("/profile");
const { next, error } = await searchParams;
return ( return (
<main className="flex min-h-screen items-center justify-center bg-neutral-50 px-6 py-12"> <main className="flex min-h-screen items-center justify-center bg-neutral-50 px-6 py-12">
<LoginForm next={next} /> <LoginForm next={next} initialError={error} />
</main> </main>
); );
} }
+26 -9
View File
@@ -1,10 +1,13 @@
"use server"; "use server";
import { redirect } from "next/navigation";
import { revalidatePath } from "next/cache";
import { signup } from "@/utils/authentication/register"; import { signup } from "@/utils/authentication/register";
export type RegisterState = { export type RegisterState =
error?: string; | { error?: string }
} | null; | { success: true; emailConfirmationNeeded: boolean }
| null;
export async function registerAction( export async function registerAction(
_prevState: RegisterState, _prevState: RegisterState,
@@ -17,15 +20,29 @@ export async function registerAction(
return { error: "Email and password are required." }; return { error: "Email and password are required." };
} }
let result;
try { try {
const result = await signup(email, password); result = await signup(email, password);
if (result?.error) {
return { error: result.error.message ?? "Registration failed." };
}
} catch (error) { } catch (error) {
const message = error instanceof Error ? error.message : "Registration failed."; const message =
error instanceof Error
? error.message
: typeof error === "string"
? error
: "Registration failed.";
return { error: message }; return { error: message };
} }
return null; if ("error" in result) {
return { error: result.error.message ?? "Registration failed." };
}
if (result.emailConfirmationNeeded) {
return { success: true, emailConfirmationNeeded: true };
}
// Auto-confirmed — redirect to profile
// redirect() must be outside try/catch because it throws to halt execution
revalidatePath("/", "layout");
redirect("/profile");
} }
+9 -1
View File
@@ -1,6 +1,14 @@
import { redirect } from "next/navigation";
import { createClient } from "@/utils/supabase/server";
import RegisterForm from "./register-form"; import RegisterForm from "./register-form";
export default function RegisterPage() { export default async function RegisterPage() {
const supabase = await createClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (user) redirect("/profile");
return ( return (
<main className="flex min-h-screen items-center justify-center bg-neutral-50 px-6 py-12"> <main className="flex min-h-screen items-center justify-center bg-neutral-50 px-6 py-12">
<RegisterForm /> <RegisterForm />
+48 -6
View File
@@ -7,10 +7,49 @@ import { registerAction, type RegisterState } from "./actions";
const initialState: RegisterState = null; const initialState: RegisterState = null;
export default function RegisterForm() { export default function RegisterForm() {
const [state, action, pending] = useActionState( const [state, action, pending] = useActionState(registerAction, initialState);
registerAction,
initialState, // Show success state if registration succeeded and email confirmation is needed
); if (state && "success" in state && state.emailConfirmationNeeded) {
return (
<div className="w-full max-w-md space-y-6">
<div className="space-y-3 rounded-xl border border-emerald-200 bg-emerald-50 p-6">
<div className="flex items-center gap-3">
<div className="flex size-10 shrink-0 items-center justify-center rounded-full bg-emerald-100">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="size-5 text-emerald-600"
>
<path
fillRule="evenodd"
d="M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm3.857-9.809a.75.75 0 0 0-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 1 0-1.06 1.061l2.5 2.5a.75.75 0 0 0 1.137-.089l4-5.5Z"
clipRule="evenodd"
/>
</svg>
</div>
<h2 className="text-lg font-semibold text-emerald-900">
Check your email
</h2>
</div>
<p className="text-sm text-emerald-700">
We&apos;ve sent a confirmation link to your email address. Please
click the link to verify your account and start chatting.
</p>
</div>
<p className="text-sm text-neutral-600">
Already confirmed?{" "}
<Link
className="font-medium text-neutral-900 hover:underline"
href="/login"
>
Log in
</Link>
</p>
</div>
);
}
return ( return (
<div className="w-full max-w-md space-y-6"> <div className="w-full max-w-md space-y-6">
@@ -47,7 +86,7 @@ export default function RegisterForm() {
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm text-neutral-900 shadow-sm focus:border-neutral-900 focus:outline-none" className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm text-neutral-900 shadow-sm focus:border-neutral-900 focus:outline-none"
/> />
</div> </div>
{state?.error ? ( {state && "error" in state && state.error ? (
<p className="text-sm text-red-600">{state.error}</p> <p className="text-sm text-red-600">{state.error}</p>
) : null} ) : null}
<button <button
@@ -60,7 +99,10 @@ export default function RegisterForm() {
</form> </form>
<p className="text-sm text-neutral-600"> <p className="text-sm text-neutral-600">
Already have an account?{" "} Already have an account?{" "}
<Link className="font-medium text-neutral-900 hover:underline" href="/login"> <Link
className="font-medium text-neutral-900 hover:underline"
href="/login"
>
Log in Log in
</Link> </Link>
</p> </p>