-
-
Save catalinmiron/a7471d75645ba38b58aad33e333e5e8a to your computer and use it in GitHub Desktop.
Helpers for Github Sponsorware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/api/auth/[...nextauth.ts] | |
// Follow docs in nextauth | |
import { isSignedInUserSponsoringMe } from 'utils/github' | |
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next' | |
import NextAuth from 'next-auth' | |
import GithubProvider from 'next-auth/providers/github' | |
export default function Auth( | |
req: NextApiRequest, | |
res: NextApiResponse | |
): ReturnType<NextApiHandler> { | |
return NextAuth(req, res, { | |
theme: { | |
colorScheme: 'light', | |
}, | |
providers: [ | |
GithubProvider({ | |
clientId: process.env.GITHUB_ID, | |
clientSecret: process.env.GITHUB_SECRET, | |
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | |
// @ts-ignore | |
scope: 'read:user', | |
}), | |
], | |
secret: process.env.NEXTAUTH_SECRET, | |
callbacks: { | |
async redirect({ baseUrl }) { | |
return baseUrl | |
}, | |
async signIn() { | |
return true | |
}, | |
async session({ session, token }) { | |
if (token) { | |
session.isSponsor = await isSignedInUserSponsoringMe() | |
} | |
return session | |
}, | |
}, | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Send a GraphQL query to the Github API | |
*/ | |
async function queryGithubApi(query: string) { | |
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, | |
}), | |
}) | |
return await res.json() | |
} | |
/** | |
* What is the signed in user's login name? | |
*/ | |
async function getSignedInUser(): Promise<{ login: 'steveruizok ' }> { | |
const res = await queryGithubApi(` | |
query { | |
viewer { | |
login | |
} | |
}`) | |
return res?.data?.viewer | |
} | |
/** | |
* Is user with the login A sponsoring the user with the login B? | |
*/ | |
async function isASponsoringB(loginA: string, loginB: string) { | |
const res = await queryGithubApi(` | |
query { | |
user(login: "${loginB}") { | |
isSponsoredBy(accountLogin: "${loginA}") | |
} | |
}`) | |
return res?.data?.user?.isSponsoredBy | |
} | |
const whitelist = ['steveruizok'] | |
/** | |
* Is the current user sponsoring me? | |
*/ | |
export async function isSignedInUserSponsoringMe() { | |
const user = await getSignedInUser() | |
if (whitelist.includes(user.login)) return true | |
return isASponsoringB('steveruizok', user.login) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react' | |
import type { GetServerSideProps } from 'next' | |
import { getSession } from 'next-auth/react' | |
interface PageProps { | |
isSponsor: boolean | |
} | |
export default function Room({ isSponsor }: PageProps): JSX.Element { | |
return <div>{isSponsor}</div> | |
} | |
export const getServerSideProps: GetServerSideProps = async (context) => { | |
const session = await getSession(context) | |
const id = context.query.id?.toString() | |
return { | |
props: { | |
isSponsor: session?.isSponsor ?? false, | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment