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

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
2021-06-19 10:22:23 +00:00
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
2021-06-21 21:35:28 +00:00
export default function Auth(
req: NextApiRequest,
res: NextApiResponse
): ReturnType<NextApiHandler> {
2021-06-19 20:18:14 +00:00
return NextAuth(req, res, {
providers: [
Providers.GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
2021-06-20 21:05:38 +00:00
scope: 'read:user',
2021-06-19 20:18:14 +00:00
}),
],
callbacks: {
async redirect(url, baseUrl) {
return url.startsWith(baseUrl) ? url : baseUrl
},
2021-06-21 21:35:28 +00:00
async signIn(user, account, profile: any) {
2021-06-19 20:18:14 +00:00
const canLogin = await isSponsoringMe(profile?.login)
2021-06-20 20:35:25 +00:00
2021-06-19 20:18:14 +00:00
if (canLogin) {
return canLogin
} else {
return '/sponsorware'
}
},
2021-06-19 10:22:23 +00:00
},
2021-06-19 20:18:14 +00:00
})
2021-06-19 14:48:45 +00:00
}
2021-06-19 20:18:14 +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
2021-06-19 14:48:45 +00:00
}