Skip to content

Instantly share code, notes, and snippets.

@brianally
Created July 2, 2015 18:33
Show Gist options
  • Save brianally/1e11ba8c793b27302f08 to your computer and use it in GitHub Desktop.
Save brianally/1e11ba8c793b27302f08 to your computer and use it in GitHub Desktop.
Ember route showing both OAuth2 and manual login
// app/routes/login.js
import Ember from "ember";
import ENV from "../config/environment";
export default Ember.Route.extend({
setupController: function(controller, model) {
controller.set("errorMessage", null);
},
actions: {
// called by form; see:
// https://gist.github.com/brianally/300862be606a49fec8b2#file-authenticators-admin-js
adminLogin: function() {
var _this = this;
var controller = this.controllerFor("login");
var credentials = controller.getProperties("email", "password");
this.get("session").authenticate("authenticator:admin", credentials)
.then(function(resp) {
console.log("authenticated: ", _this.get("session"));
}, function(error) {
controller.set("errorMessage", error);
});
},
// button action
googleLogin: function() {
var _this = this;
this.get("session").authenticate("simple-auth-authenticator:torii", "google-oauth2")
.then(function() {
// session should now contain authorization code from provider
var secureData = _this.get("session.content.secure");
// call server to initiate token exchange from provider
var exchangeData = {
authorizationCode: secureData.authorizationCode,
redirectUri : secureData.redirectUri,
provider : 'google'
};
// Use service to get the token. The session is already technically valid by now
// so if anything goes wrong you must call invalidate()
_this.tokenService.fetch(exchangeData).then(function(response) {
if (response.success) {
_this.set("session.content.secure.access_token", response.data.token);
_this.set("session.content.secure.user", response.data.user);
// redirect to new route if desired
_this.transitionTo("somewhere-else");
}
else {
// token service received an error
console.log('tokenService.fetch success false');
console.dir(response);
_this.get("session").invalidate();
}
}, function(error) {
// token service itself failed
console.log("tokenService.fetch error", error);
_this.get("session").invalidate();
});
}, function(error) {
// something bad happened
console.log("simple-auth-authenticator:torii error", error);
_this.get("session").invalidate();
});
},
// button actions for other OAuth logins
twitterLogin: function() {
// etc.
},
linkedInLogin: function() {
// etc.
},
facebookLogin: function() {
// etc.
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment