Skip to content

Instantly share code, notes, and snippets.

@catalinmiron
Forked from steveruizok/[...nextauth].ts
Created April 12, 2022 12:50
Show Gist options
  • Select an option

  • Save catalinmiron/a7471d75645ba38b58aad33e333e5e8a to your computer and use it in GitHub Desktop.

Select an option

Save catalinmiron/a7471d75645ba38b58aad33e333e5e8a to your computer and use it in GitHub Desktop.

Revisions

  1. @steveruizok steveruizok revised this gist Feb 13, 2022. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions page.tsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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,
    },
    }
    }
  2. @steveruizok steveruizok revised this gist Feb 13, 2022. 1 changed file with 43 additions and 0 deletions.
    43 changes: 43 additions & 0 deletions [...nextauth].ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // 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
    },
    },
    })
    }
  3. @steveruizok steveruizok created this gist Feb 13, 2022.
    53 changes: 53 additions & 0 deletions github.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    /**
    * 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)
    }