Search code examples
githubgraphqlapollographcool

Github Login with Graphcool


I am attempting to implement a GitHub Login with GraphCool. So far:

Steps

  1. I've run: graphcool add-template auth/github
  2. I uncommented the links in the graphcool.yml and typs.graphql files
  3. I setup a GitHub OAuth app
  4. I added my client name and secret to local environment variables.
  5. I ran graphcool deploy

Results

Under functions it says I have loggedInUser and githubAuthentication. I also seem to have a githubUserId added to createUser

Problem

From here I am stuck. I'm not sure what to do. I am not sure where to obtain a github code. I've implemented OAuth with Github before directly via their API and with Firebase. I have the following questions:

  1. In Github, what should I set the Authorization callback URL? Doesn't that need to point to graphcool?

  2. How does the OAuth pop-up window get launched so I can approve my app?

  3. How do I obtain Github user data like displayName and photoUrl. Can I add that data to a new user when createUser is ran?

I have modified the username password template to createUsers with additional fields in the past. I know I have to modify the types and .ts files in the template. It seems like I may be able to obtain specific user details around line 29 in the githubAuthentication.ts file:

const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
const { githubCode } = event.data
const githubToken: string = await getGithubToken(githubCode)
const githubUser = await getGithubUser(githubToken)
const user: User = await getGraphcoolUser(api, githubUser.id)
  .then(r => r.User)
let userId: string | null = null

Solution

  • I am not sure where to obtain a github code.

    Short Answer: in whichever way you would normally obtain a Github code.

    Long Answer:

    You can follow this guide to see how to authorize against a Github OAuth app: https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-authorization-options-for-oauth-apps/

    Or, follow the instructions from the template README.md:

    Obtaining a test code

    First, update the configuration of the OAuth app on Github you just created:

    This being index.html:

    <html>
    
    <body>
      <script>
        // Github client id
        const client_id = '__CLIENT_ID__'
        // Will extract code from current url
        const githubCode = window.location.search.substring(1).split('&')[0].split('code=')[1]
        if (githubCode) {
          // call Graphcool authenticateGithubUser mutation
          console.log(githubCode)
        }
        function getgithubCode() {
          window.location = `https://github.com/login/oauth/authorize?client_id=${client_id}&scope=user`
        }
      </script>
      <button onclick="getgithubCode();">Authenticate with Github</button>
    </body>
    
    </html>