-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathroute.ts
More file actions
42 lines (35 loc) · 1.31 KB
/
route.ts
File metadata and controls
42 lines (35 loc) · 1.31 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
import { WorkOS } from '@workos-inc/node';
import { redirect } from 'next/navigation';
import { consumeInvitationToken } from '@/lib/invitation-token';
// This is a Next.js Route Handler.
//
// If your application is a single page app (SPA) with a separate backend you will need to:
// - create a backend endpoint to handle the request
// - adapt the code below in your endpoint
//
// Please also note that for the sake of simplicity, we directly return the user here in the query string.
// In a real application, you would probably store the user in a token (JWT)
// and store that token in your DB or use cookies.
const workos = new WorkOS(process.env.WORKOS_API_KEY);
export async function GET(request: Request) {
const code = new URL(request.url).searchParams.get('code') || '';
// Check for a stored invitation token (persisted across auth flows like password reset)
const invitationToken = await consumeInvitationToken();
let response;
try {
response = await workos.userManagement.authenticateWithCode({
clientId: process.env.WORKOS_CLIENT_ID || '',
code,
invitationToken,
});
} catch (error) {
response = error;
}
if (response) {
redirect(
`http://localhost:3000/using-your-own-ui/sign-in/google-oauth?response=${JSON.stringify(
response
)}`
);
}
}