MCPcopy Index your code
hub / github.com/nextauthjs/next-auth-example / ClientExample

Function ClientExample

components/client-example.tsx:35–110  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33}
34
35export default function ClientExample() {
36 const { data: session, status } = useSession()
37 const [apiResponse, setApiResponse] = useState("")
38
39 const makeRequestWithToken = async () => {
40 try {
41 const response = await fetch("/api/authenticated/greeting")
42 const data = await response.json()
43 setApiResponse(JSON.stringify(data, null, 2))
44 } catch (error) {
45 setApiResponse("Failed to fetch data: " + error)
46 }
47 }
48
49 return (
50 <div className="flex flex-col gap-4">
51 <h1 className="text-3xl font-bold">Client Side Rendering</h1>
52 <p>
53 This page fetches session data client side using the{" "}
54 <CustomLink href="https://nextjs.authjs.dev/react#usesession">
55 <code>useSession</code>
56 </CustomLink>{" "}
57 React Hook.
58 </p>
59 <p>
60 It needs the{" "}
61 <CustomLink href="https://react.dev/reference/rsc/use-client">
62 <code>'use client'</code>
63 </CustomLink>{" "}
64 directive at the top of the file to enable client side rendering, and
65 the{" "}
66 <CustomLink href="https://nextjs.authjs.dev/react#sessionprovider">
67 <code>SessionProvider</code>
68 </CustomLink>{" "}
69 component in{" "}
70 <strong>
71 <code>client-example/page.tsx</code>
72 </strong>{" "}
73 to provide the session data.
74 </p>
75
76 <div className="flex flex-col gap-4 rounded-md bg-gray-100 p-4">
77 <h2 className="text-xl font-bold">Third-party backend integration</h2>
78 <p>
79 Press the button to send a request to our{" "}
80 <CustomLink href="https://github.com/nextauthjs/authjs-third-party-backend">
81 <code>example backend</code>
82 </CustomLink>
83 . Read more{" "}
84 <CustomLink href="https://authjs.dev/guides/integrating-third-party-backends">
85 <code>here</code>
86 </CustomLink>
87 </p>
88 <div className="flex flex-col">
89 <Button
90 disabled={!session?.accessToken}
91 onClick={makeRequestWithToken}
92 >

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected