具有自定义服务器身份验证的Ember简单身份验证(凭据未定义)

时间:2015-02-03 21:54:54

标签: ember.js ember-cli

我是Ember的新手并尝试使用ember simple auth和ember-cli在自定义服务器中实现基本身份验证('身份验证'标头中的用户名+密码) 。问题是凭据对象未定义身份验证' CustomAuthenticator 中定义的方法。

此代码有什么问题?

应用程序/初始化/ login.js

import Ember from 'ember';
import BaseAuthenticator from 'simple-auth/authenticators/base';
import BaseAuthorizer from 'simple-auth/authorizers/base';

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

export default {
    name: 'authentication',
    before: 'simple-auth',
    initialize: function(container/*, application*/) {
        container.register('authorizer:custom', CustomAuthorizer);
        container.register('authenticator:custom', CustomAuthenticator);
    }
};

var CustomAuthorizer = BaseAuthorizer.extend({
    authorize: function(jqXHR/*, requestOptions*/) {
        if (this.get('session.isAuthenticated') && !Ember.isEmpty(this.get('session.token'))) {
            jqXHR.setRequestHeader('Authorization', 'Token: ' + this.get('session.token'));
        }
    }
});

var CustomAuthenticator = BaseAuthenticator.extend({

    tokenEndpoint: '/v1/login',

     restore: function(data) {
      return new Ember.RSVP.Promise(function(resolve, reject) {
        if (!Ember.isEmpty(data.token)) {
          resolve(data);
        } else {
          reject();
        }
      });
    },
    authenticate: function(credentials) {
        //*** HERE THE CREDENTIALS OBJECT IS NULL ***
        var _this = this;
        if(!Ember.isEmpty(credentials.identification)) {                               
           return this._super(credentials); 
        } else {
            return new Ember.RSVP.Promise(function(resolve, reject) {
                Ember.$.ajax({
                    url:         _this.tokenEndpoint,
                    type:        'POST',
                    data:        JSON.stringify({ session: { identification: credentials.identification, password: credentials.password } }),
                    contentType: 'application/json'
                }).then(function(response) {
                    Ember.run(function() {
                        resolve({ token: response.session.token });
                    });
                }, function(xhr/*, status, error*/) {
                    var response = JSON.parse(xhr.responseText);
                    Ember.run(function() {
                        reject(response.error);
                    });
                });
            });
        }
    },
    invalidate: function() {
      var _this = this;
      return new Ember.RSVP.Promise(function(resolve) {
        Ember.$.ajax({ url: _this.tokenEndpoint, type: 'DELETE' }).always(function() {
          resolve();
        });
      });
    },
});

应用程序/荚/登录/ controller.js

import AuthenticationControllerMixin from 'simple-auth/mixins/authentication-controller-mixin';
import Ember from 'ember';

export default Ember.Controller.extend(AuthenticationControllerMixin, {
    authenticator: 'authenticator:custom'
});

应用程序/荚/登录/ template.hbs

<div class='container'>

    <form {{action 'authenticate' on='submit'}}>
        <label for="identification">Login</label>
        {{input value=identification placeholder='Enter Login'}}
        <label for="password">Password</label>
        {{input value=password placeholder='Enter Password' type='password'}}
        <button type="submit">Login</button>
    </form>
    {{#if errorMessage}}
        <div class="alert alert-danger">
          <p>
            <strong>Login failed:</strong> <code>{{errorMessage}}</code>
          </p>
        </div>
    {{/if}}
</div>

1 个答案:

答案 0 :(得分:1)

import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';

而不是

import AuthenticationControllerMixin from 'simple-auth/mixins/authentication-controller-mixin';