stringify user data before commiting to local storage

This commit is contained in:
Oliver Bryan
2025-12-22 13:43:12 +00:00
parent dbec5dac6c
commit e2bb57cb35

View File

@@ -2,7 +2,7 @@ import { type ChangeEvent, useState } from "react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { capitalise, cn, getAuthHeaders } from "@/lib/utils"; import { capitalise, cn } from "@/lib/utils";
function Field({ function Field({
label = "label", label = "label",
@@ -63,7 +63,7 @@ export default function LogInForm() {
fetch(`${serverURL}/auth/login`, { fetch(`${serverURL}/auth/login`, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json", ...getAuthHeaders() }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }), body: JSON.stringify({ username, password }),
}) })
@@ -72,7 +72,7 @@ export default function LogInForm() {
setError(""); setError("");
const data = await res.json(); const data = await res.json();
localStorage.setItem("token", data.token); localStorage.setItem("token", data.token);
localStorage.setItem("user", data.user); localStorage.setItem("user", JSON.stringify(data.user));
window.location.href = ""; window.location.href = "";
} }
// unauthorized // unauthorized
@@ -104,7 +104,7 @@ export default function LogInForm() {
fetch(`${serverURL}/auth/register`, { fetch(`${serverURL}/auth/register`, {
method: "POST", method: "POST",
headers: { "Content-type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({
name, name,
username, username,
@@ -116,7 +116,7 @@ export default function LogInForm() {
setError(""); setError("");
const data = await res.json(); const data = await res.json();
localStorage.setItem("token", data.token); localStorage.setItem("token", data.token);
localStorage.setItem("user", data.user); localStorage.setItem("user", JSON.stringify(data.user));
window.location.href = ""; window.location.href = "";
} }
// bad request (probably a bad user input) // bad request (probably a bad user input)