55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { AUTH_COOKIE, CMS_URL } from "@/lib/api";
|
|
import { cookies } from "next/headers";
|
|
import { NextResponse } from "next/server";
|
|
|
|
export async function GET() {
|
|
const store = await cookies();
|
|
const token = store.get(AUTH_COOKIE)?.value;
|
|
if (!token) return NextResponse.json({ user: null }, { status: 401 });
|
|
|
|
if (token === "demo-token") {
|
|
return NextResponse.json({
|
|
user: { id: "demo", email: "patient@example.com", name: "Пациент", phone: "+7" },
|
|
});
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`${CMS_URL}/api/users/me`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
cache: "no-store",
|
|
});
|
|
const data = await res.json();
|
|
return NextResponse.json(data, { status: res.status });
|
|
} catch {
|
|
return NextResponse.json({
|
|
user: { id: "demo", email: "patient@example.com", name: "Пациент" },
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function PATCH(request: Request) {
|
|
const store = await cookies();
|
|
const token = store.get(AUTH_COOKIE)?.value;
|
|
if (!token) return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
|
|
|
|
const body = await request.json();
|
|
if (token === "demo-token") {
|
|
return NextResponse.json({ user: { id: "demo", ...body } });
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`${CMS_URL}/api/users/me`, {
|
|
method: "PATCH",
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
const data = await res.json();
|
|
return NextResponse.json(data, { status: res.status });
|
|
} catch {
|
|
return NextResponse.json({ user: { id: "demo", ...body } });
|
|
}
|
|
}
|