fix: registration — parse AuthResponse correctly, auto-login after successful registration
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m38s
test / unit (pull_request) Successful in 16m14s
test / integration (pull_request) Failing after 17m16s

This commit is contained in:
2026-05-14 18:13:57 +02:00
parent e61e5b4cec
commit fcbd132a78
2 changed files with 9 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ import {
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { RegisterSchema, registerUser } from "@/lib/api"; import { RegisterSchema, registerUser } from "@/lib/api";
import Cookies from "js-cookie";
import { useState } from "react"; import { useState } from "react";
export default function RegisterPage() { export default function RegisterPage() {
@@ -37,9 +38,9 @@ export default function RegisterPage() {
async function onSubmit(values: z.infer<typeof RegisterSchema>) { async function onSubmit(values: z.infer<typeof RegisterSchema>) {
try { try {
setError(null); setError(null);
await registerUser(values); const { token } = await registerUser(values);
// You can automatically log the user in here or just redirect them Cookies.set("auth_token", token, { expires: 7, secure: true });
router.push("/login"); router.push("/");
} catch { } catch {
setError("Username or email may already be taken."); setError("Username or email may already be taken.");
} }

View File

@@ -168,7 +168,11 @@ async function apiFetch<T>(
// ── Auth ────────────────────────────────────────────────────────────────── // ── Auth ──────────────────────────────────────────────────────────────────
export const registerUser = (data: z.infer<typeof RegisterSchema>) => export const registerUser = (data: z.infer<typeof RegisterSchema>) =>
apiFetch("/auth/register", { method: "POST", body: JSON.stringify(data) }, UserSchema); apiFetch(
"/auth/register",
{ method: "POST", body: JSON.stringify(data) },
z.object({ token: z.string(), user: UserSchema })
);
export const loginUser = (data: z.infer<typeof LoginSchema>) => export const loginUser = (data: z.infer<typeof LoginSchema>) =>
apiFetch("/auth/login", { method: "POST", body: JSON.stringify(data) }, z.object({ token: z.string() })); apiFetch("/auth/login", { method: "POST", body: JSON.stringify(data) }, z.object({ token: z.string() }));