Parcourir la source

Test sponsor

main
Steve Ruiz il y a 4 ans
Parent
révision
2c61afd628
3 fichiers modifiés avec 68 ajouts et 50 suppressions
  1. 51
    13
      pages/api/auth/[...nextauth].ts
  2. 1
    37
      pages/auth-test.tsx
  3. 16
    0
      pages/sponsorware.tsx

+ 51
- 13
pages/api/auth/[...nextauth].ts Voir le fichier

1
 import { NextApiRequest, NextApiResponse } from 'next'
1
 import { NextApiRequest, NextApiResponse } from 'next'
2
 import NextAuth from 'next-auth'
2
 import NextAuth from 'next-auth'
3
+import { signin } from 'next-auth/client'
3
 import Providers from 'next-auth/providers'
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
 }

+ 1
- 37
pages/auth-test.tsx Voir le fichier

22
         <button onClick={() => signOut()}>Sign Out</button>
22
         <button onClick={() => signOut()}>Sign Out</button>
23
         <p>{loading && 'Loading...'}</p>
23
         <p>{loading && 'Loading...'}</p>
24
         <pre>{JSON.stringify(session, null, 2)}</pre>
24
         <pre>{JSON.stringify(session, null, 2)}</pre>
25
-        <p>Is owner? {isOwner.toString()}</p>
26
-        <p>Is sponsor? {isSponsor.toString()}</p>
27
-
28
-        {isSponsor ? (
29
-          <p>
30
-            <b>Hey, thanks for sponsoring me!</b>
31
-          </p>
32
-        ) : (
33
-          <p>
34
-            <b>
35
-              This site is just for my github sponsors.{' '}
36
-              <a
37
-                href="https://github.com/sponsors/steveruizok"
38
-                target="_blank"
39
-                rel="noopener noreferrer"
40
-              >
41
-                Sponsor here!
42
-              </a>
43
-            </b>
44
-          </p>
45
-        )}
25
+        {session && <p>Hey, you made it! Thanks for sponsoring me.</p>}
46
       </div>
26
       </div>
47
     </>
27
     </>
48
   )
28
   )
51
 export async function getServerSideProps(context: GetServerSidePropsContext) {
31
 export async function getServerSideProps(context: GetServerSidePropsContext) {
52
   const session = await getSession(context)
32
   const session = await getSession(context)
53
 
33
 
54
-  const image = session?.user?.image
55
-
56
-  const sponsors = await fetch(
57
-    'https://sponsors.trnck.dev/sponsors/steveruizok'
58
-  ).then((d) => d.json().then((d) => d.sponsors))
59
-
60
-  const sponsor = sponsors.some((sponsor: any) => sponsor.avatar === image)
61
-
62
-  console.log(
63
-    session?.user,
64
-    image,
65
-    sponsors.map((sponsor: any) => sponsor.avatar)
66
-  )
67
-
68
   return {
34
   return {
69
     props: {
35
     props: {
70
-      isOwner: session?.user?.email === 'steveruizok@gmail.com',
71
-      isSponsor: sponsor,
72
       ssrSession: session,
36
       ssrSession: session,
73
     },
37
     },
74
   }
38
   }

+ 16
- 0
pages/sponsorware.tsx Voir le fichier

1
+export default function Sponsorware() {
2
+  return (
3
+    <div>
4
+      Hey, this site is for sponsors only for the moment. Sorry! If you're
5
+      really curious,{' '}
6
+      <a
7
+        href="https://github.com/sponsors/steveruizok"
8
+        target="_blank"
9
+        rel="noopener noreferrer"
10
+      >
11
+        Sponsor me on Github
12
+      </a>{' '}
13
+      and try that again.
14
+    </div>
15
+  )
16
+}

Chargement…
Annuler
Enregistrer