0

当使用 Ember.SimpleAuth 和 Ember-CLI 编写自定义身份验证器时,自定义身份验证器的 authenticate 方法究竟需要返回什么才能将用户建立为已登录?以下是当前存在的身份验证器。我们在后端使用 phalcon rest api,所以最终似乎这个方法需要在服务器端点击那个 URL 并验证用户,但是服务器应该返回什么才能让 ember.simpleauth 做它需要做什么?

import Ember from "ember";
import App from '../app';
import Base from "simple-auth/authenticators/base";

export default Base.extend({
    tokenEndpoint: 'login',

    restore: function(data) {
        console.log('ran resotre');
    },
    authenticate: function(credentials) {
        alert(credentials.identification);
        alert(credentials.password);
    },
    invalidate: function() {
        console.log('ran invalidate');
    }
});
4

1 回答 1

0

我会阅读Ember Simple Auth - API

authenticate需要返回一个承诺。在该方法中,需要解决或拒绝该承诺。已解决的 Promise 将表明身份验证成功,而拒绝的 Promise 则表明身份验证失败。这是我构建快速authenticate函数的方式。

 authenticate: function (credentials, options) {
    return new Ember.RSVP.Promise(function (resolve, reject) {
        var loginPromise = Ember.$.post(<token url goes here>, {
            username: credentials.identification,
            password: credentials.password
        });
        loginPromise.then(function (data) {
            resolve({
                token: data.token,
                userData: data.user
            });
        }, function (error) {
            reject(error);
        });
    });
}
于 2014-11-10T19:42:42.483 回答