-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
60 lines (55 loc) · 1.49 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
56
57
58
59
60
import { createCallbackAuth } from "@octokit/auth-callback"
import NextAuth from "next-auth"
import github from "next-auth/providers/github"
import { Octokit } from "octokit"
import * as env from "@/env"
declare module "next-auth" {
interface Session {
token: string
}
}
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [github],
session: {
strategy: "jwt",
maxAge: 2592000,
},
callbacks: {
signIn: async ({ user, account }) => {
try {
if (account == null || account.access_token == null) {
return false
}
const octokit = new Octokit({
authStrategy: createCallbackAuth,
auth: { callback: () => account.access_token },
})
const cyanea = await octokit.rest.repos.getContent({
owner: env.CYANEA_EVENTS_REPO_OWNER,
repo: env.CYANEA_EVENTS_REPO_REPO,
path: "cyanea.json",
})
if (!(cyanea.data instanceof Array) && cyanea.data.type === "file") {
JSON.parse(atob(cyanea.data.content))
console.log(`authenticated user ${user.name} (${user.id})`)
return true
} else {
return false
}
} catch {
return false
}
},
jwt: async ({ token, account }) => {
token.token ??= account?.access_token
return token
},
session: async ({ session, token }) => {
return { ...session, token: token.token }
},
},
pages: {
signIn: "/",
error: "/",
},
})