Created
July 27, 2015 18:22
-
-
Save alexboots/e6db0b450f68a90442cf to your computer and use it in GitHub Desktop.
This file contains 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
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