0

PROBLEM: I don't know how to get the current session in a controller.

I have a custom authenticator, custom session, and initializer defined like so:

CUSTOM AUTHENTICATOR in ../app/authenticators/custom.js

var CustomAuthenticator = Base.extend({
  authenticate: function(credentials) {
    return new Ember.RSVP.Promise(function (resolve, reject){
      var loginPromise = Ember.$.post('/api/login', {'email':credentials.identification, 'password':credentials.password} );
      loginPromise.then(function (data){
        resolve({
            token: data.user.api_key,
            userData: data.user
        });
      }, function(error){
        reject(error);
      });
    });
  }
});

CUSTOM SESSION in ../app/sessions/custom.js

import Ember from 'ember';
import Session from 'simple-auth/session';

var CustomSession = Session.extend({
  after:'simple-auth',
  currentUser: function(){
    return this.container.lookup('ember_simple_auth:session');
  }.property('currentUser')
});

export default CustomSession;

INITIALIZER in ../app/initializers/authentication.js

import CustomAuthenticator from '../authenticators/custom';
import CustomSession from '../sessions/custom';

export default {
  name:       'authentication',
  before:     'simple-auth',
  initialize: function(container) {
    container.register('authenticator:custom', CustomAuthenticator);
    container.register('session:custom', CustomSession);
  }
};

I'm trying to get the token and userData in one of my controllers by using this.get('session') but it's giving me the following:

Class {store: Class, __ember1420041799205: "ember297", __nextSuper: undefined, __ember_meta__: Object, constructor: function…}

and I see the ember_simple_auth:session key and values in the local browser storage {"authenticator":"authenticator:custom","token":"123456789","userData":{"id":"1","email":"something@email.com","api_key":"123456789","expiry_time":"2014-12-31 14:02:56"}}

I basically need to get what's in the local storage. How do I do this?

Peter Kim
  • 193
  • 1
  • 16

1 Answers1

0

Ah, I figured out the problem. When first authenticating, the session variable was there but refreshing the page got rid of the session's content because I did not have a restore function in my authenticator.

  restore: function(data) {
    return new Ember.RSVP.Promise(function (resolve, reject){
      console.log('RESTORE');
      if(!Ember.isEmpty(data.token)) {
        console.log('Found token: ' + data.token);
        resolve(data);
      } else {
        console.log('Token Not Found!');
        reject();
      }
    });
 }
Peter Kim
  • 193
  • 1
  • 16