I am attempting to implement a GitHub Login with GraphCool. So far:
graphcool add-template auth/github
graphcool.yml
and typs.graphql
filesgraphcool deploy
Under functions it says I have loggedInUser
and githubAuthentication
. I also seem to have a githubUserId added to createUser
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:
In Github, what should I set the Authorization callback URL? Doesn't that need to point to graphcool?
How does the OAuth pop-up window get launched so I can approve my app?
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
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:
- Set the Homepage URL to http://localhost:8000/
- Set the Application Callback URL to http://localhost:8000/login.html
- Replace CLIENT_ID in login.html with the client id of your OAuth app
- Server login.html, for example by using python -m SimpleHTTPServer
- Open https://localhost:8000/login.html in a browser, open the DevTools and authenticate with your Github account
- Copy the code printed in the Console of your DevTools
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>