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
+
2
7
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 ) ;
3
16
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
+ }
7
24
8
25
if ( user ) {
9
26
return (
10
27
< Flex gap = "3" >
11
- < form
12
- action = { async ( ) => {
13
- "use server" ;
14
- await signOut ( ) ;
15
- } }
16
- >
28
+ < form action = { handleSignOutAction } >
17
29
< Button type = "submit" size = { large ? "3" : "2" } >
18
30
Sign Out
19
31
</ Button >
@@ -24,7 +36,7 @@ export async function SignInButton({ large }: { large?: boolean }) {
24
36
25
37
return (
26
38
< 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 >
28
40
</ Button >
29
41
) ;
30
42
}
0 commit comments