-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
41 lines (41 loc) · 1.2 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
import NextAuth from "next-auth";
import GitHubProvider from "next-auth/providers/github";
export const { auth, handlers, signIn, signOut } = NextAuth({
pages: {
signIn: "/auth/login",
signOut: "/auth/logout",
error: "/auth/error",
},
callbacks: {
signIn: async ({ account }) => {
const { providerAccountId: id } = account || {};
const headers = new Headers({
Authorization: `Bearer ${process.env.GITHUB_API}`,
"X-GitHub-Api-Version": "2022-11-28",
Accept: "application/vnd.github+json",
});
const username = await fetch(`https://api.github.com/user/${id}`, {
headers,
}).then((res) => res.json().then((data) => data.login));
const orgCheck = await fetch(
`https://api.github.com/orgs/z1g-project/members/${username}`,
{
headers,
},
);
console.log(
`User ${username} is trying to log in, status: ${orgCheck.status}`,
);
if (orgCheck.status === 204) {
return true;
}
return false;
},
},
providers: [
GitHubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
],
});