diff --git a/app/initializers/custom-session.js b/app/initializers/custom-session.js deleted file mode 100644 index 45b2a27..0000000 --- a/app/initializers/custom-session.js +++ /dev/null @@ -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); - } -}; diff --git a/app/pods/application/route.js b/app/pods/application/route.js index ef5b286..e3173ee 100644 --- a/app/pods/application/route.js +++ b/app/pods/application/route.js @@ -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() { diff --git a/app/pods/login/controller.js b/app/pods/login/controller.js index 144d2aa..c622ec8 100644 --- a/app/pods/login/controller.js +++ b/app/pods/login/controller.js @@ -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); diff --git a/app/utils/parse-base64.js b/app/utils/parse-base64.js new file mode 100644 index 0000000..deb8f8c --- /dev/null +++ b/app/utils/parse-base64.js @@ -0,0 +1,8 @@ +export default function parseBase64(token) { + let tokenData = atob(token.split('.')[1]); + try { + return JSON.parse(tokenData); + } catch (e) { + return tokenData; + } +} diff --git a/config/environment.js b/config/environment.js index f1f7848..05b7b4d 100644 --- a/config/environment.js +++ b/config/environment.js @@ -16,8 +16,8 @@ module.exports = function(environment) { }, podModulePrefix: 'hymenobacterdotinfo/pods', 'simple-auth': { - session: 'session:custom', authorizer: 'simple-auth-authorizer:token', + store: 'simple-auth-session-store:local-storage', }, 'simple-auth-token': { identificationField: 'email', diff --git a/tests/unit/utils/parse-base64-test.js b/tests/unit/utils/parse-base64-test.js new file mode 100644 index 0000000..c0387b4 --- /dev/null +++ b/tests/unit/utils/parse-base64-test.js @@ -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); +});