Test sponsor
This commit is contained in:
parent
9bb680bfbf
commit
2c61afd628
3 changed files with 68 additions and 50 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -22,27 +22,7 @@ export default function Home({
|
|||
<button onClick={() => signOut()}>Sign Out</button>
|
||||
<p>{loading && 'Loading...'}</p>
|
||||
<pre>{JSON.stringify(session, null, 2)}</pre>
|
||||
<p>Is owner? {isOwner.toString()}</p>
|
||||
<p>Is sponsor? {isSponsor.toString()}</p>
|
||||
|
||||
{isSponsor ? (
|
||||
<p>
|
||||
<b>Hey, thanks for sponsoring me!</b>
|
||||
</p>
|
||||
) : (
|
||||
<p>
|
||||
<b>
|
||||
This site is just for my github sponsors.{' '}
|
||||
<a
|
||||
href="https://github.com/sponsors/steveruizok"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Sponsor here!
|
||||
</a>
|
||||
</b>
|
||||
</p>
|
||||
)}
|
||||
{session && <p>Hey, you made it! Thanks for sponsoring me.</p>}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
|
16
pages/sponsorware.tsx
Normal file
16
pages/sponsorware.tsx
Normal file
|
@ -0,0 +1,16 @@
|
|||
export default function Sponsorware() {
|
||||
return (
|
||||
<div>
|
||||
Hey, this site is for sponsors only for the moment. Sorry! If you're
|
||||
really curious,{' '}
|
||||
<a
|
||||
href="https://github.com/sponsors/steveruizok"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Sponsor me on Github
|
||||
</a>{' '}
|
||||
and try that again.
|
||||
</div>
|
||||
)
|
||||
}
|
Loading…
Reference in a new issue