diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index ceb91a7b2..822a4487a 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -1,21 +1,59 @@ import { NextApiRequest, NextApiResponse } from 'next' import NextAuth from 'next-auth' +import { signin } from 'next-auth/client' import Providers from 'next-auth/providers' -const options = { - providers: [ - Providers.GitHub({ - clientId: process.env.GITHUB_ID, - clientSecret: process.env.GITHUB_SECRET, - }), - ], - callbacks: { - async redirect(url: string, baseUrl: string) { - return url.startsWith(baseUrl) ? url : baseUrl +export default function (req: NextApiRequest, res: NextApiResponse) { + return NextAuth(req, res, { + providers: [ + Providers.GitHub({ + clientId: process.env.GITHUB_ID, + clientSecret: process.env.GITHUB_SECRET, + }), + ], + callbacks: { + async redirect(url, baseUrl) { + return url.startsWith(baseUrl) ? url : baseUrl + }, + async session(session, token) { + // @ts-ignore + session.user.id = token.id + return session + }, + async signIn(user, account, profile) { + // @ts-ignore + const canLogin = await isSponsoringMe(profile?.login) + if (canLogin) { + return canLogin + } else { + return '/sponsorware' + } + }, }, - }, + }) } -export default function (req: NextApiRequest, res: NextApiResponse) { - return NextAuth(req, res, options) +const whitelist = ['steveruizok'] + +async function isSponsoringMe(login: string) { + if (whitelist.includes(login)) return true + + const res = await fetch('https://api.github.com/graphql', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'bearer ' + process.env.GITHUB_API_SECRET, + }, + body: JSON.stringify({ + query: ` + query { + user(login: "steveruizok") { + isSponsoredBy(accountLogin: "${login}") + } + } + `, + }), + }).then((res) => res.json()) + + return res?.data?.user?.isSponsoredBy } diff --git a/pages/auth-test.tsx b/pages/auth-test.tsx index f27409b88..f20e7d784 100644 --- a/pages/auth-test.tsx +++ b/pages/auth-test.tsx @@ -22,27 +22,7 @@ export default function Home({
{loading && 'Loading...'}
{JSON.stringify(session, null, 2)}-
Is owner? {isOwner.toString()}
-Is sponsor? {isSponsor.toString()}
- - {isSponsor ? ( -- Hey, thanks for sponsoring me! -
- ) : ( -- - This site is just for my github sponsors.{' '} - - Sponsor here! - - -
- )} + {session &&Hey, you made it! Thanks for sponsoring me.
} > ) @@ -51,24 +31,8 @@ export default function Home({ export async function getServerSideProps(context: GetServerSidePropsContext) { const session = await getSession(context) - const image = session?.user?.image - - const sponsors = await fetch( - 'https://sponsors.trnck.dev/sponsors/steveruizok' - ).then((d) => d.json().then((d) => d.sponsors)) - - const sponsor = sponsors.some((sponsor: any) => sponsor.avatar === image) - - console.log( - session?.user, - image, - sponsors.map((sponsor: any) => sponsor.avatar) - ) - return { props: { - isOwner: session?.user?.email === 'steveruizok@gmail.com', - isSponsor: sponsor, ssrSession: session, }, } diff --git a/pages/sponsorware.tsx b/pages/sponsorware.tsx new file mode 100644 index 000000000..dd9eafe8f --- /dev/null +++ b/pages/sponsorware.tsx @@ -0,0 +1,16 @@ +export default function Sponsorware() { + return ( +