-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.ts
39 lines (36 loc) · 1004 Bytes
/
auth.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
import { ConvexAdapter } from "./app/ConvexAdapter";
import GitHubProvider from "next-auth/providers/github";
import { SignJWT, importPKCS8 } from "jose";
import NextAuth from "next-auth";
const CONVEX_SITE_URL = process.env.NEXT_PUBLIC_CONVEX_URL!.replace(
/.cloud$/,
".site"
);
export const { handlers, signIn, signOut, auth } = NextAuth({
debug: true,
providers: [GitHubProvider],
adapter: ConvexAdapter,
callbacks: {
async session({ session }) {
const privateKey = await importPKCS8(
process.env.CONVEX_AUTH_PRIVATE_KEY!,
"RS256"
);
const convexToken = await new SignJWT({
sub: session.userId,
})
.setProtectedHeader({ alg: "RS256" })
.setIssuedAt()
.setIssuer(CONVEX_SITE_URL)
.setAudience("convex")
.setExpirationTime("1h")
.sign(privateKey);
return { ...session, convexToken };
},
},
});
declare module "next-auth" {
interface Session {
convexToken: string;
}
}