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, {
 | 
					export default Ember.Route.extend(ApplicationRouteMixin, {
 | 
				
			||||||
  actions: {
 | 
					  actions: {
 | 
				
			||||||
    invalidateSession: function() {
 | 
					    invalidateSession: function() {
 | 
				
			||||||
      this.get('session').invalidate();
 | 
					      this.get('session').invalidate().then(() => {
 | 
				
			||||||
 | 
					        // Wait until promise is resolved
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    didTransition: function() {
 | 
					    didTransition: function() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,16 +1,22 @@
 | 
				
			||||||
import Ember from 'ember';
 | 
					import Ember from 'ember';
 | 
				
			||||||
 | 
					import parseBase64 from '../../utils/parse-base64';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default Ember.Controller.extend({
 | 
					export default Ember.Controller.extend({
 | 
				
			||||||
  actions: {
 | 
					  actions: {
 | 
				
			||||||
    authenticate: function() {
 | 
					    authenticate: function() {
 | 
				
			||||||
      let credentials = this.getProperties('identification', 'password');
 | 
					      let credentials = this.getProperties('identification', 'password');
 | 
				
			||||||
 | 
					      let session = this.get('session');
 | 
				
			||||||
      let authenticator = 'simple-auth-authenticator:token';
 | 
					      let authenticator = 'simple-auth-authenticator:token';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      this.set('loading', true);
 | 
					      this.set('loading', true);
 | 
				
			||||||
      // Manually clean up because there might not be a transition
 | 
					      // Manually clean up because there might not be a transition
 | 
				
			||||||
      this.get('flashMessages').clearMessages();
 | 
					      this.get('flashMessages').clearMessages();
 | 
				
			||||||
      this.get('session').authenticate(authenticator, credentials).then(()=>{
 | 
					      session.authenticate(authenticator, credentials).then(()=>{
 | 
				
			||||||
        this.set('loading', false);
 | 
					        this.set('loading', false);
 | 
				
			||||||
 | 
					        let t = parseBase64(session.get('secure.token'));
 | 
				
			||||||
 | 
					        this.store.find('user', t['sub']).then((user) => {
 | 
				
			||||||
 | 
					          session.set('currentUser', user);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
      }, (error)=> {
 | 
					      }, (error)=> {
 | 
				
			||||||
        this.set('loading', false);
 | 
					        this.set('loading', false);
 | 
				
			||||||
        this.get('flashMessages').error(error.error);
 | 
					        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;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -16,8 +16,8 @@ module.exports = function(environment) {
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    podModulePrefix: 'hymenobacterdotinfo/pods',
 | 
					    podModulePrefix: 'hymenobacterdotinfo/pods',
 | 
				
			||||||
    'simple-auth': {
 | 
					    'simple-auth': {
 | 
				
			||||||
      session: 'session:custom',
 | 
					 | 
				
			||||||
      authorizer: 'simple-auth-authorizer:token',
 | 
					      authorizer: 'simple-auth-authorizer:token',
 | 
				
			||||||
 | 
					      store: 'simple-auth-session-store:local-storage',
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    'simple-auth-token': {
 | 
					    'simple-auth-token': {
 | 
				
			||||||
      identificationField: 'email',
 | 
					      identificationField: 'email',
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										10
									
								
								tests/unit/utils/parse-base64-test.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								tests/unit/utils/parse-base64-test.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,10 @@
 | 
				
			||||||
 | 
					import parseBase64 from '../../../utils/parse-base64';
 | 
				
			||||||
 | 
					import { module, test } from 'qunit';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module('Unit | Utility | parse base64');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Replace this with your real tests.
 | 
				
			||||||
 | 
					test('it works', function(assert) {
 | 
				
			||||||
 | 
					  var result = parseBase64();
 | 
				
			||||||
 | 
					  assert.ok(result);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
		Reference in a new issue