mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
Next.js 13.4 [is out](https://nextjs.org/blog/next-13-4). For discussing project-related issues, please use https://github.com/nextauthjs/next-auth/discussions/8487 The new version of NextAuth.js is based on `@auth/core`. If you want to test it out, you can do so already, installing `next-auth@experimental`: - **Documentation**: https://authjs.dev/reference/nextjs - **Migration guide**: https://authjs.dev/guides/upgrade-to-v5 BREAKING CHANGE: Follow the [migration guide](https://authjs.dev/guides/upgrade-to-v5)
82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
"use client"
|
|
|
|
import { useSession } from "next-auth/react"
|
|
import { Button } from "./ui/button"
|
|
import { Input } from "./ui/input"
|
|
import { useState } from "react"
|
|
import SessionData from "./session-data"
|
|
import CustomLink from "./custom-link"
|
|
|
|
const UpdateForm = () => {
|
|
const { data: session, update } = useSession()
|
|
const [name, setName] = useState(session?.user.name ?? "")
|
|
|
|
if (!session) return null
|
|
return (
|
|
<>
|
|
<h2 className="text-xl font-bold">Updating the session</h2>
|
|
<form
|
|
onSubmit={async () => {
|
|
if (session) {
|
|
const newSession = await update({
|
|
...session,
|
|
user: { ...session.user, name },
|
|
})
|
|
console.log({ newSession })
|
|
}
|
|
}}
|
|
className="flex items-center w-full max-w-sm space-x-2"
|
|
>
|
|
<Input
|
|
type="text"
|
|
placeholder={session.user.name ?? ""}
|
|
value={name}
|
|
onChange={(e) => {
|
|
setName(e.target.value)
|
|
}}
|
|
/>
|
|
<Button type="submit">Update</Button>
|
|
</form>
|
|
</>
|
|
)
|
|
}
|
|
|
|
export default function ClientExample() {
|
|
const { data: session, status } = useSession()
|
|
return (
|
|
<div className="space-y-2">
|
|
<h1 className="text-3xl font-bold">Client Side Rendering Usage</h1>
|
|
<p>
|
|
This page fetches session data client side using the{" "}
|
|
<CustomLink href="https://nextjs.authjs.dev/react#usesession">
|
|
<code>useSession</code>
|
|
</CustomLink>{" "}
|
|
React Hook.
|
|
</p>
|
|
<p>
|
|
It needs the{" "}
|
|
<CustomLink href="https://react.dev/reference/react/use-client">
|
|
<code>'use client'</code>
|
|
</CustomLink>{" "}
|
|
directive at the top of the file to enable client side rendering, and
|
|
the{" "}
|
|
<CustomLink href="https://nextjs.authjs.dev/react#sessionprovider">
|
|
<code>SessionProvider</code>
|
|
</CustomLink>{" "}
|
|
component in{" "}
|
|
<strong>
|
|
<code>client-example/page.tsx</code>
|
|
</strong>{" "}
|
|
to provide the session data.
|
|
</p>
|
|
|
|
{status === "loading" ? (
|
|
<div>Loading...</div>
|
|
) : (
|
|
<SessionData session={session} />
|
|
)}
|
|
<UpdateForm />
|
|
</div>
|
|
)
|
|
}
|