-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.ts
55 lines (53 loc) · 1.39 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { PrismaClient } from "@prisma/client";
import { decrypt, encrypt } from "@/lib/config/encrypt";
const prisma = new PrismaClient();
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [
GitHub({
clientId: process.env.AUTH_GITHUB_ID,
clientSecret: process.env.AUTH_GITHUB_SECRET,
authorization: {
params: {
scope: "public_repo, user:email",
},
},
}),
],
events: {
async linkAccount({
user,
account,
profile,
}: {
user: any;
account: any;
profile?: any;
}) {
const updatedAccount = {
...account,
access_token: account.access_token
? encrypt(account.access_token)
: undefined,
refresh_token: account.refresh_token
? encrypt(account.refresh_token)
: undefined,
};
await prisma.account.update({
where: {
provider_providerAccountId: {
provider: account.provider,
providerAccountId: account.providerAccountId,
},
},
data: {
access_token: updatedAccount.access_token,
refresh_token: updatedAccount.refresh_token,
},
});
},
},
});