|
|
@@ -1,21 +1,59 @@
|
|
1
|
1
|
import { NextApiRequest, NextApiResponse } from 'next'
|
|
2
|
2
|
import NextAuth from 'next-auth'
|
|
|
3
|
+import { signin } from 'next-auth/client'
|
|
3
|
4
|
import Providers from 'next-auth/providers'
|
|
4
|
5
|
|
|
5
|
|
-const options = {
|
|
6
|
|
- providers: [
|
|
7
|
|
- Providers.GitHub({
|
|
8
|
|
- clientId: process.env.GITHUB_ID,
|
|
9
|
|
- clientSecret: process.env.GITHUB_SECRET,
|
|
10
|
|
- }),
|
|
11
|
|
- ],
|
|
12
|
|
- callbacks: {
|
|
13
|
|
- async redirect(url: string, baseUrl: string) {
|
|
14
|
|
- return url.startsWith(baseUrl) ? url : baseUrl
|
|
|
6
|
+export default function (req: NextApiRequest, res: NextApiResponse) {
|
|
|
7
|
+ return NextAuth(req, res, {
|
|
|
8
|
+ providers: [
|
|
|
9
|
+ Providers.GitHub({
|
|
|
10
|
+ clientId: process.env.GITHUB_ID,
|
|
|
11
|
+ clientSecret: process.env.GITHUB_SECRET,
|
|
|
12
|
+ }),
|
|
|
13
|
+ ],
|
|
|
14
|
+ callbacks: {
|
|
|
15
|
+ async redirect(url, baseUrl) {
|
|
|
16
|
+ return url.startsWith(baseUrl) ? url : baseUrl
|
|
|
17
|
+ },
|
|
|
18
|
+ async session(session, token) {
|
|
|
19
|
+ // @ts-ignore
|
|
|
20
|
+ session.user.id = token.id
|
|
|
21
|
+ return session
|
|
|
22
|
+ },
|
|
|
23
|
+ async signIn(user, account, profile) {
|
|
|
24
|
+ // @ts-ignore
|
|
|
25
|
+ const canLogin = await isSponsoringMe(profile?.login)
|
|
|
26
|
+ if (canLogin) {
|
|
|
27
|
+ return canLogin
|
|
|
28
|
+ } else {
|
|
|
29
|
+ return '/sponsorware'
|
|
|
30
|
+ }
|
|
|
31
|
+ },
|
|
15
|
32
|
},
|
|
16
|
|
- },
|
|
|
33
|
+ })
|
|
17
|
34
|
}
|
|
18
|
35
|
|
|
19
|
|
-export default function (req: NextApiRequest, res: NextApiResponse) {
|
|
20
|
|
- return NextAuth(req, res, options)
|
|
|
36
|
+const whitelist = ['steveruizok']
|
|
|
37
|
+
|
|
|
38
|
+async function isSponsoringMe(login: string) {
|
|
|
39
|
+ if (whitelist.includes(login)) return true
|
|
|
40
|
+
|
|
|
41
|
+ const res = await fetch('https://api.github.com/graphql', {
|
|
|
42
|
+ method: 'POST',
|
|
|
43
|
+ headers: {
|
|
|
44
|
+ 'Content-Type': 'application/json',
|
|
|
45
|
+ Authorization: 'bearer ' + process.env.GITHUB_API_SECRET,
|
|
|
46
|
+ },
|
|
|
47
|
+ body: JSON.stringify({
|
|
|
48
|
+ query: `
|
|
|
49
|
+ query {
|
|
|
50
|
+ user(login: "steveruizok") {
|
|
|
51
|
+ isSponsoredBy(accountLogin: "${login}")
|
|
|
52
|
+ }
|
|
|
53
|
+ }
|
|
|
54
|
+ `,
|
|
|
55
|
+ }),
|
|
|
56
|
+ }).then((res) => res.json())
|
|
|
57
|
+
|
|
|
58
|
+ return res?.data?.user?.isSponsoredBy
|
|
21
|
59
|
}
|