Created
January 13, 2024 08:26
-
-
Save borispoehland/212ee00972e86a5c912bb31ab4d08efd to your computer and use it in GitHub Desktop.
Next.js 14 + React RSC Signout flow using efficient data fetching with <Await />
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Thenable } from "react"; | |
export async function Await<T>({ | |
promise, | |
children, | |
}: { | |
promise: Thenable<T>; | |
children: (value: T) => JSX.Element; | |
}) { | |
const data = await promise; | |
return children(data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import { SubmitButton } from "@/components/SubmitButton"; | |
import { Session } from "next-auth"; | |
import { signOut } from "next-auth/react"; | |
import { useRouter } from "next/navigation"; | |
export function SignOut({ auth }: { auth: Session | null }) { | |
const router = useRouter(); | |
if (!auth) { | |
// or sign in button | |
return <></>; | |
} | |
return ( | |
<form | |
action={async () => { | |
await signOut({ redirect: false }); | |
router.refresh(); | |
}} | |
> | |
<SubmitButton>Sign out</SubmitButton> | |
</form> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment