【问题标题】:Emberjs authentication session not workingEmberjs 身份验证会话不起作用
【发布时间】:2014-01-31 21:46:07
【问题描述】:

我关注了Authentication Tutorial,但遇到了一些问题。

我有一个驻留在另一个域中的 php 后端 API,http://rest.api {local development} ember js 应用程序使用 ember-app-kit 并连接到 rest api。 当用户提交登录表单时,它将带有密码的用户名/电子邮件发送到其余 api Session Controller 中定义的路由之一@

import AuthManager from 'lms/config/auth_manager';

var SessionNewController = Ember.ObjectController.extend({

attemptedTransition : null,
loginText : 'Log In',

actions: {
    loginUser : function() {
        var self = this;
        var router = this.get('target');
        var data = this.getProperties('identity', 'password');
        var attemptedTrans = this.get('attemptedTransition');

        $.post('http://rest.api/login',
            data,
            function(results) {
                console.log(results.session);
                console.log(results.user_id);
                AuthManager.authenticate(results.session, results.user_id);
                if(attemptedTrans) {
                    attemptedTrans.retry();
                    self.set('attemptedTransition', null);
                } else {
                    router.transitionTo('index');
                }
            }
        )
    }
}


});

export default SessionNewController;

在结果变量中接收到 api 结果后,如下所示:

Object {success: "user login success", session: "2OmwKLPclC.YhYAT3745467my7t0m2uo", user_id: "1"}

但只要我捕获数据并将其发送到位于Auth Manager Code 中的 AuthManager

import User from 'lms/models/user';
import Application from 'lms/adapters/application';
var AuthManager = Ember.Object.extend({

    init: function() {
        this._super();
        var accessToken = $.cookie('access_token');
        var authUserId = $.cookie('auth_user');
        if(!Ember.isEmpty(accessToken) || !Ember.isEmpty(authUserId)) {
            this.authenticate(accessToken, authUserId);
        }
    },

    isAuthenticated: function() {
        return !Ember.isEmpty(this.get('ApiKey.accessToken')) && !Ember.isEmpty(this.get('ApiKey.user'));
    },

    authenticate: function(accessToken, userId) {
        $.ajaxSetup({
            headers: { 'Authorization': 'Bearer ' + accessToken }
        });
        var user = User.store.find(userId);
        console.log(user);
        this.set('ApiKey', ApiKey.create({
            accessToken: accessToken,
            user: user
        }));
    },

    reset: function() {
        this.set('ApiKey', null);
        $.ajaxSetup({
            headers: { 'Authorization': 'Bearer None' }
        });
    },

    apiKeyObserver: function() {
        Application.accessToken = this.get('apikey.accessToken');
        if (Ember.isEmpty(this.get('ApiKey'))) {
            $.removeCookie('access_token');
            $.removeCookie('auth_user');
        } else {
            $.cookie('access_token', this.get('ApiKey.accessToken'));
            $.cookie('auth_user', this.get('ApiKey.user.id'));
        }
    }.observes('ApiKey')
});

export default AuthManager;

控制台出现错误提示

Uncaught TypeError: Object function () {
    if (!wasApplied) {
      Class.proto(); // prepare prototype...
    }
    o_defineProperty(this, GUID_KEY, undefinedDescriptor);
    o_defineProperty(this, '_super', undefinedDescriptor);
    var m = met...<omitted>...e' new.js:23
(anonymous function) new.js:23
jQuery.Callbacks.fire jquery.js:1037
jQuery.Callbacks.self.fireWith jquery.js:1148
done jquery.js:8074
jQuery.ajaxTransport.send.callback jquery.js:8598

无法将变量传递给导入的函数。

【问题讨论】:

    标签: javascript rest session authentication ember.js


    【解决方案1】:

    终于搞定了。我所做的错误是在 auth_manager.js 上扩展 Ember.Object.extend() 之后,我没有在任何地方创建对象。这就是它无法设置创建 cookie 并抛出该错误消息的原因。

    我所要做的就是在扩展对象之后 .create() 。

    不知道这个方法对不对。但它确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2020-10-06
      • 2018-10-23
      相关资源
      最近更新 更多