Created
November 26, 2013 15:17
-
-
Save elis/7660160 to your computer and use it in GitHub Desktop.
Login/Authentication Process using Ember
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
var ApplicationRoute = Ember.Route.extend({ | |
beforeModel: function () { | |
var self = this, | |
user = this.controllerFor('currentUser'); | |
if (user.get('isLoggedIn')) { | |
var verifyPromise = user.verifyToken(); | |
return verifyPromise.then(function(userName) { | |
self.transitionTo('/'); | |
}, function (error) { | |
self.transitionTo('/auth/login'); | |
}); | |
} else { | |
self.transitionTo('/auth/login'); | |
} | |
} | |
}); | |
export default ApplicationRoute; |
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
var CurrentUser = Ember.Controller.extend({ | |
firstName: '', | |
lastName: '', | |
email: '', | |
currentProject: null, | |
currentTable: null, | |
activeToken: null, | |
isLoggedIn: Ember.computed.notEmpty('activeToken'), | |
fullName: function () { | |
return this.firstName + ' ' + this.lastName; | |
}.property('firstName', 'lastName'), | |
init: function () { | |
try { | |
var user = JSON.parse(localStorage.currentUser); | |
this.setProperties(user); | |
} catch (err) {} | |
try { | |
var token = JSON.parse(localStorage.activeToken); | |
this.set('activeToken', token); | |
} catch (err) {} | |
}, | |
verifyToken: function () { | |
var self = this; | |
var token = this.get('activeToken'); | |
var promise = Ember.RSVP.Promise(function(resolve, reject) { | |
gapi.auth.setToken(token); | |
self.loadUserDetails().then(resolve, function() { | |
self.authenticateUser(true).then(resolve, reject); | |
}); | |
}); | |
return promise; | |
}, | |
loadUserDetails: function () { | |
var self = this; | |
var promise = Ember.RSVP.Promise(function(resolve, reject) { | |
gapi.client.load('oauth2','v2', function () { | |
var request = gapi.client.oauth2.userinfo.get(); | |
request.execute(function(result) { | |
if (!result.error) { | |
self.updateUserDetails(result); | |
resolve(); | |
} else { | |
reject(result.message); | |
} | |
}); | |
}); | |
}); | |
return promise; | |
}, | |
updateUserDetails: function (newDetails) { | |
this.setProperties({ | |
firstName: newDetails.given_name, | |
lastName: newDetails.family_name, | |
email: newDetails.email | |
}); | |
localStorage.currentUser = JSON.stringify(this.serialize()); | |
}, | |
authenticateUser: function (immediate) { | |
var self = this; | |
var promise = Ember.RSVP.Promise(function (resolve, reject) { | |
var clientId = 'Some Client ID'; // WebApp | |
var config = { | |
'client_id': clientId, | |
'immediate': !!immediate, | |
'scope': ['https://www.googleapis.com/auth/bigquery', | |
'https://www.googleapis.com/auth/plus.me', | |
'https://www.googleapis.com/auth/userinfo.profile', | |
'https://www.googleapis.com/auth/userinfo.email'] | |
}; | |
gapi.auth.authorize(config, function (token) { | |
self.set('activeToken', token); | |
localStorage.activeToken = JSON.stringify(token); | |
resolve(token); | |
}); | |
}); | |
return promise; | |
}, | |
actions: { | |
login: function () { | |
var self = this; | |
self.authenticateUser().then(function() { | |
self.transitionToRoute('/'); | |
}); | |
} | |
}, | |
serialize: function () { | |
return { | |
firstName: this.get('firstName'), | |
lastName: this.get('lastName'), | |
email: this.get('email'), | |
currentProject: this.get('currentProject'), | |
currentTable: this.get('currentTable') | |
}; | |
} | |
}); | |
export default CurrentUser; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment