Replace Clerk with self-contained JWT auth (bcryptjs + jose), remove all external auth dependencies. Add full i18n system with Chinese as default language and English toggle — React Context + JSON dictionaries, no external i18n library. Auth: - Add passwordHash to User model, generate JWT secret - Create /api/auth/register, login, logout, me endpoints - Rewrite proxy.ts middleware for custom session validation - Add AuthProvider + useAuth hook, sign-in/sign-up pages - Wire auth into layout and all API routes i18n: - React Context I18nProvider with localStorage + cookie persistence - dictionaries/zh.ts (default) and dictionaries/en.ts - LanguageSwitcher component, server-side createServerT() utility - CJK font stack via [lang="zh"] CSS selector - HTML lang attribute synced on language change - All pages, components, TabBar, StageBadge translated - Engine fallback strings (reflection-analyzer, summary-generator) - API routes pass locale from cookie to engines Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { getSession } from "@/lib/auth";
|
|
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
export async function GET() {
|
|
const session = await getSession();
|
|
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
const userId = session.userId;
|
|
|
|
const state = await prisma.userState.findUnique({
|
|
where: { userId },
|
|
select: {
|
|
attentionLevel: true,
|
|
emotionStability: true,
|
|
stressLevel: true,
|
|
practiceStage: true,
|
|
consistencyScore: true,
|
|
streakDays: true,
|
|
totalSessionsCompleted: true,
|
|
onboardingCompleted: true,
|
|
lastSessionDate: true,
|
|
},
|
|
});
|
|
|
|
if (!state) {
|
|
return NextResponse.json({
|
|
state: null,
|
|
hasStarted: false,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({
|
|
state,
|
|
hasStarted: true,
|
|
});
|
|
}
|
|
|
|
export async function PATCH(req: Request) {
|
|
const session = await getSession();
|
|
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
const userId = session.userId;
|
|
|
|
const body = await req.json().catch(() => null);
|
|
if (!body || typeof body !== "object") {
|
|
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
|
}
|
|
|
|
const { onboardingCompleted } = body as { onboardingCompleted?: boolean };
|
|
|
|
const state = await prisma.userState.upsert({
|
|
where: { userId },
|
|
create: {
|
|
userId,
|
|
onboardingCompleted: onboardingCompleted ?? false,
|
|
},
|
|
update: {
|
|
...(onboardingCompleted !== undefined ? { onboardingCompleted } : {}),
|
|
},
|
|
select: {
|
|
attentionLevel: true,
|
|
emotionStability: true,
|
|
stressLevel: true,
|
|
practiceStage: true,
|
|
consistencyScore: true,
|
|
streakDays: true,
|
|
totalSessionsCompleted: true,
|
|
onboardingCompleted: true,
|
|
lastSessionDate: true,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ state });
|
|
}
|