Upgrading authentication libs
This commit is contained in:
		
							parent
							
								
									878e8d1b60
								
							
						
					
					
						commit
						459bd7d6be
					
				
					 7 changed files with 36 additions and 30 deletions
				
			
		| 
						 | 
				
			
			@ -1,5 +1,5 @@
 | 
			
		|||
// from: http://blog.willrax.com/fetching-the-current-user-with-simple-auth/
 | 
			
		||||
import Ember from "ember";
 | 
			
		||||
import DS from 'ember-data';
 | 
			
		||||
import Session from "simple-auth/session";
 | 
			
		||||
 | 
			
		||||
// This is pulled straight from ember-cli-simple-auth-token
 | 
			
		||||
| 
						 | 
				
			
			@ -12,22 +12,23 @@ function getTokenData(token) {
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var CustomSession = Session.extend({
 | 
			
		||||
  currentUser: function() {
 | 
			
		||||
    let token = this.get('secure.token');
 | 
			
		||||
    if (!Ember.isEmpty(token)) {
 | 
			
		||||
      let t = getTokenData(token);
 | 
			
		||||
      return DS.PromiseObject.create({
 | 
			
		||||
        promise: this.container.lookup('store:main').find('user', t['sub'])
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
    return null;
 | 
			
		||||
  }.property('token')
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
export default {
 | 
			
		||||
  name: "custom-session",
 | 
			
		||||
  before: "simple-auth",
 | 
			
		||||
  initialize: function(container) {
 | 
			
		||||
    Session.reopen({
 | 
			
		||||
      setCurrentUser: function() {
 | 
			
		||||
        var token = this.get("token");
 | 
			
		||||
        var self = this;
 | 
			
		||||
 | 
			
		||||
        if (!Ember.isEmpty(token)) {
 | 
			
		||||
          var t = getTokenData(token);
 | 
			
		||||
          return container.lookup("store:main").find("user", t['sub']).then(function(user) {
 | 
			
		||||
            self.set("currentUser", user);
 | 
			
		||||
          });
 | 
			
		||||
        }
 | 
			
		||||
      }.observes("token")
 | 
			
		||||
    });
 | 
			
		||||
  initialize: function(container, application) {
 | 
			
		||||
    application.register('session:custom', CustomSession);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,9 @@ import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
 | 
			
		|||
 | 
			
		||||
export default Ember.Route.extend(ApplicationRouteMixin, {
 | 
			
		||||
  actions: {
 | 
			
		||||
    invalidateSession: function() {
 | 
			
		||||
      this.get('session').invalidate();
 | 
			
		||||
    },
 | 
			
		||||
    loading: function() {
 | 
			
		||||
      NProgress.start();
 | 
			
		||||
      this.router.one('didTransition', function() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,20 +1,18 @@
 | 
			
		|||
import Ember from 'ember';
 | 
			
		||||
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';
 | 
			
		||||
 | 
			
		||||
export default Ember.Controller.extend(LoginControllerMixin, {
 | 
			
		||||
  authenticator: 'simple-auth-authenticator:jwt',
 | 
			
		||||
  loading: false,
 | 
			
		||||
export default Ember.Controller.extend({
 | 
			
		||||
  actions: {
 | 
			
		||||
    authenticate: function() {
 | 
			
		||||
      this.set('errorMessage', null);
 | 
			
		||||
      var _this = this;
 | 
			
		||||
      let credentials = this.getProperties('identification', 'password');
 | 
			
		||||
      let authenticator = 'simple-auth-authenticator:token';
 | 
			
		||||
 | 
			
		||||
      this.set('loading', true);
 | 
			
		||||
      this._super().then(function() {
 | 
			
		||||
        _this.set('loading', false);
 | 
			
		||||
      }, function(error) {
 | 
			
		||||
        _this.set('loading', false);
 | 
			
		||||
        var message = error.error;
 | 
			
		||||
        _this.set('errorMessage', message);
 | 
			
		||||
      this.get('session').authenticate(authenticator, credentials).then(()=>{
 | 
			
		||||
        this.set('loading', false);
 | 
			
		||||
      }, (error)=> {
 | 
			
		||||
        this.set('loading', false);
 | 
			
		||||
        this.set('errorMessage', error.error);
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
import Ember from 'ember';
 | 
			
		||||
import UnauthenticatedRouteMixin from 'simple-auth/mixins/unauthenticated-route-mixin';
 | 
			
		||||
 | 
			
		||||
export default Ember.Route.extend({
 | 
			
		||||
export default Ember.Route.extend(UnauthenticatedRouteMixin, {
 | 
			
		||||
  setupController: function(controller) {
 | 
			
		||||
    controller.set('errorMessage', null);
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue