Skip to content

Instantly share code, notes, and snippets.

@constellates
Last active June 16, 2017 13:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save constellates/a76132ed7de74610a435 to your computer and use it in GitHub Desktop.
Save constellates/a76132ed7de74610a435 to your computer and use it in GitHub Desktop.
React OAuth 2 user store with Hello.js
// dependencies ----------------------------------------------------------------------
import Reflux from 'reflux';
import Actions from '../actions/Actions';
import request from 'superagent';
import config from '../config';
let UserStore = Reflux.createStore({
// store setup -----------------------------------------------------------------------
listenables: Actions,
/**
* Init
*
* Initializes hello.js with the google client ID when
* the store initializes.
*/
init: function () {
hello.init({google: config.auth.google.clientID});
},
// Actions ---------------------------------------------------------------------------
/**
* Log In
*
* Starts the google OAuth2 sign in flow.
*/
signIn: function () {
let self = this;
hello('google').login({scope: 'email,openid'}, function (userdata) {
self.data = userdata; // ???????????????????????????????????????????????????
console.log('signin success');
}, function () {
console.log('signin failure');
});
},
/**
* Sign Out
*
* Signs the user out by destroying the current
* OAuth2 session.
*/
signOut: function () {
hello('google').logout().then(function () {
console.log('signout success');
}, function (e) {
console.log('signout failure');
console.log(e);
});
},
/**
* Log Token
*
* Logs the current google access token stored in local storage.
*/
logToken: function () {
console.log(JSON.parse(window.localStorage.hello).google.access_token);
},
/**
* Get User Data
*
*/
getUserData: function () {
return this.data; // ???????????????????????????????????????????????????
}
});
export default UserStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment