Drop custom session
This commit is contained in:
		
							parent
							
								
									6f6faee122
								
							
						
					
					
						commit
						4c8cbd20c7
					
				
					 6 changed files with 30 additions and 37 deletions
				
			
		| 
						 | 
				
			
			@ -1,34 +0,0 @@
 | 
			
		|||
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
 | 
			
		||||
function getTokenData(token) {
 | 
			
		||||
  var tokenData = atob(token.split('.')[1]);
 | 
			
		||||
  try {
 | 
			
		||||
    return JSON.parse(tokenData);
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    return tokenData;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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, application) {
 | 
			
		||||
    application.register('session:custom', CustomSession);
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
| 
						 | 
				
			
			@ -4,7 +4,10 @@ import ApplicationRouteMixin from 'simple-auth/mixins/application-route-mixin';
 | 
			
		|||
export default Ember.Route.extend(ApplicationRouteMixin, {
 | 
			
		||||
  actions: {
 | 
			
		||||
    invalidateSession: function() {
 | 
			
		||||
      this.get('session').invalidate();
 | 
			
		||||
      this.get('session').invalidate().then(() => {
 | 
			
		||||
        // Wait until promise is resolved
 | 
			
		||||
        return true;
 | 
			
		||||
      });
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    didTransition: function() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,16 +1,22 @@
 | 
			
		|||
import Ember from 'ember';
 | 
			
		||||
import parseBase64 from '../../utils/parse-base64';
 | 
			
		||||
 | 
			
		||||
export default Ember.Controller.extend({
 | 
			
		||||
  actions: {
 | 
			
		||||
    authenticate: function() {
 | 
			
		||||
      let credentials = this.getProperties('identification', 'password');
 | 
			
		||||
      let session = this.get('session');
 | 
			
		||||
      let authenticator = 'simple-auth-authenticator:token';
 | 
			
		||||
 | 
			
		||||
      this.set('loading', true);
 | 
			
		||||
      // Manually clean up because there might not be a transition
 | 
			
		||||
      this.get('flashMessages').clearMessages();
 | 
			
		||||
      this.get('session').authenticate(authenticator, credentials).then(()=>{
 | 
			
		||||
      session.authenticate(authenticator, credentials).then(()=>{
 | 
			
		||||
        this.set('loading', false);
 | 
			
		||||
        let t = parseBase64(session.get('secure.token'));
 | 
			
		||||
        this.store.find('user', t['sub']).then((user) => {
 | 
			
		||||
          session.set('currentUser', user);
 | 
			
		||||
        });
 | 
			
		||||
      }, (error)=> {
 | 
			
		||||
        this.set('loading', false);
 | 
			
		||||
        this.get('flashMessages').error(error.error);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										8
									
								
								app/utils/parse-base64.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								app/utils/parse-base64.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
export default function parseBase64(token) {
 | 
			
		||||
  let tokenData = atob(token.split('.')[1]);
 | 
			
		||||
  try {
 | 
			
		||||
    return JSON.parse(tokenData);
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    return tokenData;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue