Ember Simple Auth custom authenticator

我只是一个虾纸丫 提交于 2019-12-30 10:29:08

问题


I've been trying to create a session.currentUser property with an id, email, and points properties.

I'm referencing Custom authenticator with Ember simple auth + Ember CLI and How to store the user in a session, but I just can't figure out how the pieces fit together.

I'm using Ember CLI. In my index.html, I have:

window.ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:devise',
  session: 'session:custom'
};

In initializers/custom-session.js, I have:

import Session from 'simple-auth/session';
import Devise from 'simple-auth-devise/authenticators/devise';

export default {
  name: 'session:custom',
  before: 'simple-auth',
  initialize: function(container) {
    container.register('session:custom', Devise);
    Session.extend({
      currentUser: function() {
        var id = this.get('id');
        var email: this.get('user_email');
        var points: this.get('points');

        if (!Ember.isEmpty(id)) {
          return this.store.createRecord('user', {
            id: id,
            email: email,
            points: points
          });
        }
      }.property('id')
    });
  }
};

This feels wrong to me in many ways, but after several hours of trying to make this work, it's at least an indication of what I'm trying to accomplish.

I'd be super grateful for any help. Thank you in advance!


回答1:


I got it!!! :)

index.html:

window.ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:devise',
  session: 'session:withCurrentUser'
};

initializers/customize-session.js:

import Session from 'simple-auth/session';

var SessionWithCurrentUser = Session.extend({
  currentUser: function() {
    var userId = this.get('user_id');
    if (!Ember.isEmpty(userId)) {
      return this.container.lookup('store:main').find('user', userId);
    }
  }.property('user_id')
});

export default {
  name: 'customize-session',
  initialize: function(container) {
    container.register('session:withCurrentUser', SessionWithCurrentUser);
  }
};

Hope this helps someone else.



来源:https://stackoverflow.com/questions/25298436/ember-simple-auth-custom-authenticator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!