How to save current user data in EmberJS?

I have an EmberJS application built using ember-cli. I am currently using simple-auth with a user authenticator.

In the authenticator, when the user logs in, I want to save his data in order to use it later. I have the following code:

authenticate: function(options) {
  var self = this;
  return new Ember.RSVP.Promise(function(resolve, reject){
    API.user.login(options.username, options.password, true).done(function(data) {
      // @TODO: Save current user
      resolve(data.id);
    }).fail(function() {
      reject();
    });
  });
},

User data is available in the variable data.user.

I tried to use Ember.set('App.currentUser', data.user);, but it does not work. What should I do?

+4
source share
2 answers

I finished creating the user controller Sessionsand set the current user object there, and then created an alias from the application controller.

- .

0

, . , , , , user_email API

//initializers/session-user.js

import Ember from "ember";
import Session from "simple-auth/session";
export function initialize(container) {
  Session.reopen({
    setCurrentUser: function() {
      var accessToken = this.get('access_token');
      var self = this;
        if (!Ember.isEmpty(accessToken)) {
          return container.lookup('store:main').find('user', {
            email: self.get('user_email')
          }).then(function (users){
            self.set('currentUser', users.get('firstObject'));
          });
        }
      }.observes('access_token')
   });
}

export default {
  name: 'session-user',
  before: 'simple-auth',
  initialize: initialize
};

, : http://discuss.emberjs.com/t/best-practice-for-loading-and-persisting-current-user-in-an-authenticated-system/6987

simple-auth > 0.8.0-beta.1,

+1

All Articles