Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created July 8, 2021 13:20
Show Gist options
  • Save steveruizok/be1d6cfbce3c19ed90b5ba046e2754f8 to your computer and use it in GitHub Desktop.
Save steveruizok/be1d6cfbce3c19ed90b5ba046e2754f8 to your computer and use it in GitHub Desktop.
Sponsorware with next-auth.
// pages/api/auth/[...nextAuth].ts
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
export default function Auth(
req: NextApiRequest,
res: NextApiResponse
): ReturnType<NextApiHandler> {
return NextAuth(req, res, {
providers: [
Providers.GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
scope: 'read:user',
}),
],
callbacks: {
async redirect(url, baseUrl) {
return baseUrl
},
async signIn(user, account, profile: any) {
const canLogin = await isSponsoringMe(profile?.login)
if (canLogin) {
return canLogin
} else {
return '/sponsorware'
}
},
},
})
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment