-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathpage.tsx
More file actions
53 lines (45 loc) · 1.58 KB
/
page.tsx
File metadata and controls
53 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { WorkOS } from '@workos-inc/node';
import { getUser, signOut } from './auth';
// This example uses Next.js with React Server Components.
// Because this page is an RSC, the code stays on the server, which allows
// us to use the WorkOS Node SDK without exposing our API key to the client.
//
// If your application is a single page app (SPA), you will need to:
// - create a form that can POST to an endpoint in your backend
// - call the `getAuthorizationURL` method in that endpoint
// - redirect the user to the returned URL
const workos = new WorkOS(process.env.WORKOS_API_KEY);
export default async function WithSession() {
const { isAuthenticated, user } = await getUser();
const authKitUrl = workos.userManagement.getAuthorizationUrl({
clientId: process.env.WORKOS_CLIENT_ID || '',
provider: 'authkit',
redirectUri: 'http://localhost:3000/using-hosted-authkit/with-session/callback',
});
return (
<main>
<h1>With session</h1>
{isAuthenticated ? (
<>
<h2>Welcome back{user?.firstName && `, ${user?.firstName}`}</h2>
<p>You are now authenticated into the application.</p>
<form
action={async () => {
'use server';
await signOut();
}}
>
<button type="submit">Sign-out</button>
</form>
</>
) : (
<>
<h2>Sign-in</h2>
<p>Sign-in to view your account details</p>
<a href={authKitUrl}>Sign-in</a>
</>
)}
<pre>{JSON.stringify({ user }, null, 2)}</pre>
</main>
);
}