app/dashboard/page.js fetched the profile with the standalone uuid cookie, which still holds the OTP uuid and 404s. Read the user uuid from the userInfo cookie (falling back to the uuid cookie); the backend resolves it and lazy-creates the profile. Also fix buildPatientUser to read the double-nested response (data.data) and the profile's label field. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
import Content from "@/components/dashboard/Content";
|
|
import { defineAbilitiesFor } from "@/lib/ability";
|
|
import { getUser } from "@/lib/auth";
|
|
import { getStateInfo } from "@/lib/getStateInfo";
|
|
import { removeToken } from "@/utils";
|
|
import { cookies } from "next/headers";
|
|
import { redirect } from "next/navigation";
|
|
import { fetchReq } from "@/lib/req";
|
|
import { safeJsonParse } from "@/lib/sanitize";
|
|
import { buildPatientUser } from "@/lib/representationAdapters";
|
|
|
|
export default async function Dashboard({ searchParams }) {
|
|
const awaitedSearchParams = await searchParams;
|
|
const { matchedCity } = await getStateInfo();
|
|
const user = await getUser();
|
|
|
|
const ability = defineAbilitiesFor(user);
|
|
const cookieStore = await cookies();
|
|
const token = cookieStore.get("access_token");
|
|
|
|
if (!ability.can("access", "Dashboard")) {
|
|
removeToken();
|
|
return redirect("/login");
|
|
}
|
|
|
|
// The userInfo cookie holds the real user uuid; the standalone `uuid` cookie
|
|
// may still carry the OTP uuid, which 404s against user-profile.
|
|
const userInfo = safeJsonParse(cookieStore.get("userInfo")?.value);
|
|
const userUuid = userInfo?.uuid || cookieStore.get("uuid")?.value;
|
|
let profile = null;
|
|
if (userUuid) {
|
|
profile = await fetchReq(
|
|
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/user-profile/${userUuid}`,
|
|
{ headers: { Authorization: `Bearer ${token.value}` } }
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Content
|
|
logged={token.value}
|
|
params={awaitedSearchParams}
|
|
matchedCity={matchedCity}
|
|
user={buildPatientUser(profile)}
|
|
/>
|
|
);
|
|
}
|