mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-06-28 06:34:15 +00:00
parent
2209a17687
commit
4bdfefc1c6
17 changed files with 410 additions and 66 deletions
|
|
@ -7,6 +7,12 @@ datasource db {
|
|||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
enum Theme {
|
||||
LIGHT
|
||||
DARK
|
||||
SYSTEM
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
|
|
@ -14,6 +20,7 @@ model User {
|
|||
emailVerified DateTime?
|
||||
image String?
|
||||
description String? @db.VarChar(512)
|
||||
theme Theme @default(SYSTEM)
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
|
|
|||
53
backend/src/app/api/auth/theme/route.ts
Normal file
53
backend/src/app/api/auth/theme/route.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import z from "zod";
|
||||
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { RateLimit } from "@/lib/rate-limit";
|
||||
|
||||
const themeSchema = z.enum(["LIGHT", "DARK", "SYSTEM"]);
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const session = await auth();
|
||||
if (!session || !session.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: Number(session.user.id) },
|
||||
select: { theme: true },
|
||||
});
|
||||
|
||||
if (!user) return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||
|
||||
return NextResponse.json({ theme: user.theme });
|
||||
} catch (error) {
|
||||
console.error("Failed to get theme:", error);
|
||||
return NextResponse.json({ error: "Failed to get theme" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const session = await auth();
|
||||
if (!session || !session.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const rateLimit = new RateLimit(request, 5);
|
||||
const check = await rateLimit.handle();
|
||||
if (check) return check;
|
||||
|
||||
const { theme } = await request.json();
|
||||
|
||||
const validation = themeSchema.safeParse(theme);
|
||||
if (!validation.success) return rateLimit.sendResponse({ error: "Invalid theme value" }, 400);
|
||||
|
||||
try {
|
||||
await prisma.user.update({
|
||||
where: { id: Number(session.user.id) },
|
||||
data: { theme: validation.data },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Failed to update theme:", error);
|
||||
return rateLimit.sendResponse({ error: "Failed to update theme" }, 500);
|
||||
}
|
||||
|
||||
return rateLimit.sendResponse({ success: true });
|
||||
}
|
||||
|
|
@ -39,6 +39,8 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
|||
if (user) {
|
||||
session.user.id = user.id;
|
||||
session.user.email = user.email;
|
||||
// @ts-expect-error - theme is added to User model
|
||||
session.user.theme = user.theme;
|
||||
}
|
||||
return session;
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue