Skip to content

Commit b4eccf7

Browse files
committed
Add example of client component auth
1 parent 9e245d3 commit b4eccf7

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

src/app/actions/getSignInUrl.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use server";
2+
3+
import { getSignInUrl } from "@workos-inc/authkit-nextjs";
4+
5+
export const getSignInUrlAction = async () => {
6+
return await getSignInUrl();
7+
};

src/app/actions/signOut.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use server";
2+
3+
import { signOut } from "@workos-inc/authkit-nextjs";
4+
5+
export const handleSignOutAction = async () => {
6+
await signOut();
7+
};
Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import { getSignInUrl, withAuth, signOut } from "@workos-inc/authkit-nextjs";
1+
"use client";
2+
3+
/**
4+
* Example of a client component using the useAuth hook to get the current user session.
5+
*/
6+
27
import { Button, Flex } from "@radix-ui/themes";
8+
import { useAuth } from "@workos-inc/authkit-nextjs/components";
9+
import { useEffect, useState } from "react";
10+
import { getSignInUrlAction } from "../actions/getSignInUrl";
11+
import { handleSignOutAction } from "../actions/signOut";
12+
13+
export function SignInButton({ large }: { large?: boolean }) {
14+
const { user, loading } = useAuth();
15+
const [signInUrl, setSignInUrl] = useState<string | undefined>(undefined);
316

4-
export async function SignInButton({ large }: { large?: boolean }) {
5-
const { user } = await withAuth();
6-
const authorizationUrl = await getSignInUrl();
17+
useEffect(() => {
18+
getSignInUrlAction().then(setSignInUrl);
19+
}, []);
20+
21+
if (loading) {
22+
return <div>Loading...</div>;
23+
}
724

825
if (user) {
926
return (
1027
<Flex gap="3">
11-
<form
12-
action={async () => {
13-
"use server";
14-
await signOut();
15-
}}
16-
>
28+
<form action={handleSignOutAction}>
1729
<Button type="submit" size={large ? "3" : "2"}>
1830
Sign Out
1931
</Button>
@@ -24,7 +36,7 @@ export async function SignInButton({ large }: { large?: boolean }) {
2436

2537
return (
2638
<Button asChild size={large ? "3" : "2"}>
27-
<a href={authorizationUrl}>Sign In {large && "with AuthKit"}</a>
39+
<a href={signInUrl}>Sign In {large && "with AuthKit"}</a>
2840
</Button>
2941
);
3042
}

0 commit comments

Comments
 (0)