Skip to content

Instantly share code, notes, and snippets.

@alexboots
Created July 27, 2015 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexboots/e6db0b450f68a90442cf to your computer and use it in GitHub Desktop.
Save alexboots/e6db0b450f68a90442cf to your computer and use it in GitHub Desktop.
routes: {
'login': 'login',
'logout': 'logout',
'auth/callback?:query': 'authCallback' // :query syntax passes whatever str is passed back to authCallback
},
//////////
//////////
//////////
login () {
const state = uuid() // lib that uses browsers cripto object to generate a unique identifier
window.localStorage.state = state
// Lets use qs module to build query strings instead of doing '?client_id=' + someID + '&moreStuff' + ::and so on
// has stringify and parse method just like json
window.location = 'https://github.com/login/oauth/authorize?' + qs.stringify({
client_id: '6cc6877e2fb34e439f4d', // From github app
redirect_uri: window.location.origin + '/auth/callback',
scope: 'user, repo', // this is defining whats accessible (like in facbeook where its all 'this app has access to email, wall posts' etc)
state: state
// Generate random string to pass to api - api then sends string back w the same string.
// This is a way to nobody messed with your request
})
},
authCallback (query) {
query = qs.parse(query)
if(query.state === window.localStorage.state) {
console.log('authed', query.code)
delete window.localStorage.state
xhr({ //just a ajax api, could use jquery, idk
url: 'https://alexboots-ampersand-react-base.herokuapp.com/authenticate/' + query.code,
json: true, //will parse response
},
(error, resp, body) => {
if(error){
console.error('An error has occured')
} else {
this.redirectTo('/repos')
app.me.token = body.token // session var for me model
}
console.log('error', error)
console.log('resp', resp)
console.log('body', body.token)
console.log('token', body.token)
});
}
},
logout () {
window.localStorage.clear() // could also inform user they're logging out of this app, not github as a whole. idk
window.location = '/' // force full page refresh to clear anything in memory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment