-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.ts
49 lines (40 loc) · 1.32 KB
/
route.ts
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
import { createRouteHandlerClient } from "@supabase/auth-helpers-nextjs";
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
import { signToken } from "@statebacked/token";
import type { AuthContext } from "../../../statebacked/src/auth-context";
const stateBackedKeyId = process.env.STATEBACKED_KEY_ID!;
const stateBackedSecretKey = process.env.STATEBACKED_SECRET_KEY!;
export const dynamic = "force-dynamic";
export async function GET() {
const supabase = createRouteHandlerClient({ cookies });
const userData = await supabase.auth.getUser();
if (userData.error) {
return NextResponse.json(null, { status: 403 });
}
const user = userData.data.user;
if (!user.email) {
return NextResponse.json(null, { status: 403 });
}
// This is the authorization context that our State Backed authorizers can use
// to allow requests to read and write to our machine instances.
// See statebacked/src/index.ts for the authorizers.
const authContext: AuthContext = {
sub: user.id,
email: user.email,
};
const token = await signToken(
{
stateBackedKeyId,
stateBackedSecretKey,
},
authContext,
{
expires: {
in: "7 days",
},
issuer: "email-automation.examples.statebacked.dev",
},
);
return NextResponse.json({ token });
}