fix(dashboard): fetch profile by real user uuid, not OTP uuid
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>
This commit is contained in:
@@ -6,6 +6,7 @@ import { removeToken } from "@/utils";
|
|||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { fetchReq } from "@/lib/req";
|
import { fetchReq } from "@/lib/req";
|
||||||
|
import { safeJsonParse } from "@/lib/sanitize";
|
||||||
import { buildPatientUser } from "@/lib/representationAdapters";
|
import { buildPatientUser } from "@/lib/representationAdapters";
|
||||||
|
|
||||||
export default async function Dashboard({ searchParams }) {
|
export default async function Dashboard({ searchParams }) {
|
||||||
@@ -22,11 +23,14 @@ export default async function Dashboard({ searchParams }) {
|
|||||||
return redirect("/login");
|
return redirect("/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
const uuid = cookieStore.get("uuid")?.value;
|
// 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;
|
let profile = null;
|
||||||
if (uuid) {
|
if (userUuid) {
|
||||||
profile = await fetchReq(
|
profile = await fetchReq(
|
||||||
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/user-profile/${uuid}`,
|
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/user-profile/${userUuid}`,
|
||||||
{ headers: { Authorization: `Bearer ${token.value}` } }
|
{ headers: { Authorization: `Bearer ${token.value}` } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,11 +52,12 @@ export function adaptRepresentationDashboard(dashboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function buildPatientUser(profile) {
|
export function buildPatientUser(profile) {
|
||||||
const data = profile?.data ?? profile ?? {};
|
// GET /user-profile is double-nested: { data: { data: {...} } } (after fetchReq → response.data)
|
||||||
|
const data = profile?.data?.data ?? profile?.data ?? profile ?? {};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
profile: data.avatar ?? "/assets/images/profile-user.png",
|
profile: data.avatar ?? "/assets/images/profile-user.png",
|
||||||
name: [data.name, data.family].filter(Boolean).join(" ").trim(),
|
name: [data.label ?? data.name, data.family].filter(Boolean).join(" ").trim(),
|
||||||
phone: data.phone ?? data.mobile ?? "",
|
phone: data.phone ?? data.mobile ?? "",
|
||||||
total_transactions: 0,
|
total_transactions: 0,
|
||||||
number_of_turns: 0,
|
number_of_turns: 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user