Every Leaf and Kettle member has a public profile at /members/<handle>. The page renders whatever getProfileDTO(handle) gives it, and right now that's the whole database row: the password hash, the phone number, and a note staff wrote about the customer are all in the page sent to the browser. Write getProfileDTO in lib/dto.ts, the data transfer object the page and anything else showing a profile will use.
Find the member with findMemberByHandle(handle) from @/lib/db; return null when there's none (the page answers 404). Find out who's asking with getViewer() from @/lib/session, which gives { id, role } from the session cookie, or null for a guest.
Return a new object with exactly these six fields. handle, name, and favouriteTeas go to everyone. email is the member's email when the viewer is that member or staff (role: "staff"), and null for everyone else. staffNote is the note for staff only, and null for everyone else, the member included. isYou is whether the viewer is this member.
Nothing else leaves the function, whoever asks: not the passwordHash, the phone, the role, the id, or any column added to the table later.
getProfileDTO("ana") as a guest
→ { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"], email: null, staffNote: null, isYou: false }
getProfileDTO("ana") as Sam, staff
→ { ..., email: "ana@example.com", staffNote: "Prefers deliveries after 5pm.", isYou: false }
lib/db.ts, read-only. Import it as @/lib/db.
import "server-only";
export type MemberRow = {
id: string;
handle: string;
name: string;
email: string;
phone: string;
passwordHash: string;
role: "customer" | "staff";
favouriteTeas: string[];
staffNote: string;
lastLoginIp: string;
};
/** Every member, with every column: a stand-in for the shop's database. */
export const members: MemberRow[] = [
{
id: "u_ana",
handle: "ana",
name: "Ana Lima",
email: "ana@example.com",
phone: "+44 7700 900123",
passwordHash: "$argon2id$v=19$m=65536,t=3,p=4$c2FsdC1hbmE$aGFzaC1hbmE",
role: "customer",
favouriteTeas: ["Green tea", "Mint tea"],
staffNote: "Prefers deliveries after 5pm.",
lastLoginIp: "203.0.113.24",
},
{
id: "u_ben",
handle: "ben",
name: "Ben Okafor",
email: "ben@example.com",
phone: "+44 7700 900456",
passwordHash: "$argon2id$v=19$m=65536,t=3,p=4$c2FsdC1iZW4$aGFzaC1iZW4",
role: "customer",
favouriteTeas: ["Oolong tea"],
staffNote: "",
lastLoginIp: "198.51.100.7",
},
{
id: "u_sam",
handle: "sam",
name: "Sam Reyes",
email: "sam@leafandkettle.example",
phone: "+44 7700 900789",
passwordHash: "$argon2id$v=19$m=65536,t=3,p=4$c2FsdC1zYW0$aGFzaC1zYW0",
role: "staff",
favouriteTeas: ["Lapsang souchong"],
staffNote: "Runs the Bath shop.",
lastLoginIp: "192.0.2.81",
},
];
/** The member with this handle, the whole row, or undefined. */
export async function findMemberByHandle(handle: string): Promise<MemberRow | undefined> {
const member = members.find((entry) => entry.handle === handle);
return member && { ...member, favouriteTeas: [...member.favouriteTeas] };
}
lib/session.ts, read-only. Import it as @/lib/session.
import { cookies } from "next/headers";
export type Viewer = { id: string; role: "customer" | "staff" };
/** Live sessions: the token in the `session` cookie, and whose it is. */
const sessions = new Map<string, Viewer>([
["token-ana", { id: "u_ana", role: "customer" }],
["token-ben", { id: "u_ben", role: "customer" }],
["token-sam", { id: "u_sam", role: "staff" }],
]);
/** Who sent this request, from the `session` cookie, or null for a guest. */
export async function getViewer(): Promise<Viewer | null> {
const token = (await cookies()).get("session")?.value;
const viewer = token ? sessions.get(token) : undefined;
return viewer ? { ...viewer } : null;
}
app/members/[handle]/page.tsx, read-only. Import it as @/app/members/[handle]/page.
import { notFound } from "next/navigation";
import { getProfileDTO } from "@/lib/dto";
export default async function MemberPage({ params }: { params: Promise<{ handle: string }> }) {
const { handle } = await params;
const profile = await getProfileDTO(handle);
if (!profile) notFound();
return (
<main>
<h1>{profile.name}</h1>
<h2>Favourite teas</h2>
<ul>
{profile.favouriteTeas.map((tea) => (
<li key={tea}>{tea}</li>
))}
</ul>
{profile.email !== null && <p>Email: {profile.email}</p>}
{profile.staffNote !== null && <p>Staff note: {profile.staffNote}</p>}
{profile.isYou && <a href="/account/profile">Edit your profile</a>}
</main>
);
}
const as = (token, work) => withRequest({ cookies: token ? { session: token } : {} }, work);
const profileFor = (token, handle) => as(token, () => getProfileDTO(handle));
const ana = { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"] };
expect(await profileFor(null, "ana")).toEqual({ ...ana, email: null, staffNote: null, isYou: false });
expect(await profileFor("token-forged", "ana")).toEqual({ ...ana, email: null, staffNote: null, isYou: false });
const as = (token, work) => withRequest({ cookies: token ? { session: token } : {} }, work);
const profileFor = (token, handle) => as(token, () => getProfileDTO(handle));
const ana = { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"] };
expect(await profileFor("token-ana", "ana")).toEqual({ ...ana, email: "ana@example.com", staffNote: null, isYou: true });
const as = (token, work) => withRequest({ cookies: token ? { session: token } : {} }, work);
const profileFor = (token, handle) => as(token, () => getProfileDTO(handle));
const ana = { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"] };
expect(await profileFor("token-ben", "ana")).toEqual({ ...ana, email: null, staffNote: null, isYou: false });
expect(await profileFor("token-ana", "ben")).toEqual({ handle: "ben", name: "Ben Okafor", favouriteTeas: ["Oolong tea"], email: null, staffNote: null, isYou: false });
const as = (token, work) => withRequest({ cookies: token ? { session: token } : {} }, work);
const profileFor = (token, handle) => as(token, () => getProfileDTO(handle));
const ana = { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"] };
expect(await profileFor("token-sam", "ana")).toEqual({ ...ana, email: "ana@example.com", staffNote: "Prefers deliveries after 5pm.", isYou: false });
expect((await profileFor("token-sam", "ben")).staffNote).toBe("");
expect(await profileFor("token-sam", "sam")).toEqual({
handle: "sam",
name: "Sam Reyes",
favouriteTeas: ["Lapsang souchong"],
email: "sam@leafandkettle.example",
staffNote: "Runs the Bath shop.",
isYou: true,
});
const as = (token, work) => withRequest({ cookies: token ? { session: token } : {} }, work);
const profileFor = (token, handle) => as(token, () => getProfileDTO(handle));
const ana = { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"] };
for (const token of [null, "token-ana", "token-ben", "token-sam"]) {
for (const handle of ["ana", "ben", "sam"]) {
const profile = await profileFor(token, handle);
expect(Object.keys(profile).sort()).toEqual(["email", "favouriteTeas", "handle", "isYou", "name", "staffNote"]);
expect(JSON.stringify(profile)).not.toMatch(/argon2|7700 900|u_ana|u_ben|u_sam|203\.0\.113|198\.51\.100|192\.0\.2/);
}
}
const as = (token, work) => withRequest({ cookies: token ? { session: token } : {} }, work);
const profileFor = (token, handle) => as(token, () => getProfileDTO(handle));
const ana = { handle: "ana", name: "Ana Lima", favouriteTeas: ["Green tea", "Mint tea"] };
await as("token-ben", () => renderServer(<Page params={params({ handle: "ana" })} />));
expect(screen.getByRole("heading", { level: 1, name: "Ana Lima" })).toBeInTheDocument();
expect(screen.getByText("Mint tea")).toBeInTheDocument();
expect(screen.queryByText(/ana@example\.com/)).not.toBeInTheDocument();
expect(screen.queryByText(/Staff note/)).not.toBeInTheDocument();
expect(await profileFor("token-sam", "zoe")).toBeNull();
expect(await accessErrorOf(() => as(null, () => renderServer(<Page params={params({ handle: "zoe" })} />)))).toBe(404);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with A profile that shows each viewer what they may see. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.