Skip to content

Checkpoint: Remember My Name

The Course
•
20 min study

Close the earlier lessons and build this from the brief.

The brief

  • Settings (/settings) has a Your name field and Save. Saving stores the name in a cookie and goes back to /.
  • The header says Hi, Ada once a name is saved, and Hi there before, on every page.
  • The home page's heading is Welcome back, Ada, or Welcome.
  • GET /api/me answers { "name": "Ada" }, or { "name": null }. The home page has an Ask the API button that shows The API knows you as Ada. or The API doesn't know your name yet.
  • Titles: the home page is Study Hub, and Settings is Settings · Study Hub.

Acceptance checks: press Ask the API first; save Ada in Settings; ask again.

Your workspace Next.js · 3 files
import Link from "next/link"; import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <nav> <Link href="/">Home</Link> <Link href="/settings">Settings</Link> </nav> </header> {children} </body> </html> ); } // Build the brief: a greeting from a cookie, settings, titles and an API. export default function HomePage() { return ( <main> <h1>Welcome</h1> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the files

app/settings/page.jsx and app/settings/actions.js, app/api/me/route.js, and a client component for the button. The layout reads the cookie for the greeting.

Hint 2: route handlers and cookies

A route handler can call cookies() too, just as a page can.

Show one solution
One solution Next.js · 7 files
import Link from "next/link"; import { cookies } from "next/headers"; import "./globals.css"; export const metadata = { title: { template: "%s · Study Hub", default: "Study Hub" }, }; export default async function RootLayout({ children }) { const name = (await cookies()).get("name")?.value; return ( <html lang="en"> <body> <header> <nav> <Link href="/">Home</Link> <Link href="/settings">Settings</Link> <span className="muted">{name ? `Hi, ${name}` : "Hi there"}</span> </nav> </header> {children} </body> </html> ); } import { cookies } from "next/headers"; import NameChecker from "./NameChecker.jsx"; export default async function HomePage() { const name = (await cookies()).get("name")?.value; return ( <main> <h1>{name ? `Welcome back, ${name}` : "Welcome"}</h1> <NameChecker /> </main> ); } "use client"; import { useState } from "react"; export default function NameChecker() { const [answer, setAnswer] = useState(""); async function check() { const response = await fetch("/api/me"); const { name } = await response.json(); setAnswer(name ? `The API knows you as ${name}.` : "The API doesn't know your name yet."); } return ( <p> <button onClick={check}>Ask the API</button> {answer} </p> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } "use server"; import { cookies } from "next/headers"; import { redirect } from "next/navigation"; export async function saveName(formData) { const name = String(formData.get("name") ?? "").trim().slice(0, 30); if (name) (await cookies()).set("name", name); redirect("/"); } import { saveName } from "./actions.js"; export const metadata = { title: "Settings" }; export default function SettingsPage() { return ( <main> <h1>Settings</h1> <form action={saveName}> <label> Your name <input name="name" required /> </label>{" "} <button>Save</button> </form> </main> ); } import { cookies } from "next/headers"; // GET /api/me answers with the saved name, or null. export async function GET() { const name = (await cookies()).get("name")?.value ?? null; return Response.json({ name }); }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

Check yourself

  1. Which file makes /api/me, and what does it export?
  2. Which status code means "you sent something wrong"? Which means "created"?
  3. Why read a theme from a cookie on the server rather than from localStorage?

Was this article helpful?

Your response is saved on this device.