使用Ember简单身份验证+ Ember CLI的自定义身份验证器 [英] Custom authenticator with Ember simple auth + Ember CLI

查看:123
本文介绍了使用Ember简单身份验证+ Ember CLI的自定义身份验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个自定义身份验证器,类似于文档中的此示例。目的是能够通过 session.user 检索当前登录的用户。

I'm trying to write a custom authenticator, similar to the one from this example in the docs. The goal is to be able to retrieve the currently logged in user via session.user.

我正在使用Ember CLI,所以在 initializers / authentication.js 中,我有

I'm using Ember CLI, so in initializers/authentication.js I have

import Ember from 'ember';

var customAuthenticator = Ember.SimpleAuth.Authenticators.Devise.extend({
  authenticate: function(credentials) {
    debugger;
  }
});

export default {
  name: 'authentication',

  initialize: function(container, application) {

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

    // register the custom authenticator so the session can find it
    container.register('authenticator:custom', customAuthenticator);

    Ember.SimpleAuth.setup(container, application, {
      routeAfterAuthentication: 'landing-pages',
      authorizerFactory: 'ember-simple-auth-authorizer:devise'
    });
  }
};

当我尝试进行身份验证时,出现以下错误:

When I try to authenticate, I get the following error:

TypeError: Cannot read property 'authenticate' of undefined
at __exports__.default.Ember.ObjectProxy.extend.authenticate

知道为什么吗?

推荐答案

的简单身份验证0.6.4,您现在可以执行以下操作:

As of Simple Auth 0.6.4, you can now do something like:

index.html:

index.html:

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

initializers / customize-session.js:

initializers/customize-session.js:

import Ember from 'ember';
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);
  }
};

这篇关于使用Ember简单身份验证+ Ember CLI的自定义身份验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆