tldraw/packages/www/pages/api/auth/[...nextauth].ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-09-03 14:11:50 +00:00
import type { 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) {
const login = profile?.login as string
2021-09-03 14:11:50 +00:00
if (login) {
const canLogin = await isSponsoringMe(login)
if (canLogin) {
return canLogin
}
2021-09-03 14:11:50 +00:00
}
return '/sponsorware'
2021-09-03 14:11:50 +00:00
},
},
})
}
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
}