javascript - How should I store current user details in EmberJS? -
i have emberjs application generated using ember-cli. i'm using simple-auth custom authenticator.
in authenticator, when user logs in want save details can use later. have 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 available in variable data.user.
i tried using ember.set('app.currentuser', data.user); it's not working. should do?
i think works easiest use initializer. theres several ways can resolve user, think easiest if pass user_email alongside grant token api
//initializers/session-user.js import ember "ember"; import session "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 }; check thread idea of came from: http://discuss.emberjs.com/t/best-practice-for-loading-and-persisting-current-user-in-an-authenticated-system/6987
and if using simple-auth > 0.8.0-beta.1 need adjust initializer
Comments
Post a Comment