authentication - Ember.js Ember Simple Auth 在 LocalStorage 中保留身份验证信息不起作用

标签 authentication ember.js

我使用Ember Simple Auth具有以下设置: 注意:我使用 Ember 应用程序套件。

app.js

// init Ember.SimpleAuth
App.initializer({
    name: 'authentication',
    initialize: function(container, application) {
        Ember.SimpleAuth.setup(application, { // @todo at version 0.1.2 of Ember-simple-auth, add container variable
            crossOriginWhitelist: ['http://customdomain'], 
            // store: Ember.SimpleAuth.Stores.LocalStorage, // default now
            authenticationRoute: 'article.login'
        });
    }
});

export
default App;

一个简单的登录 Controller (主要取自 Ember App Kit Simple Auth )

var CustomAuthenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend({
    serverTokenEndpoint: 'http://customdomain/access_token/',

    makeRequest: function(data) {
        return Ember.$.ajax({
            url: this.serverTokenEndpoint,
            type: 'POST',
            data: {
                grant_type: 'password',
                username: data.username,
                password: data.password
            },
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded'
        });
    }
});

var LoginController = Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMixin, {
    authenticator: CustomAuthenticator,

    actions: {
        // display an error when logging in fails
        sessionAuthenticationFailed: function(message) {
          console.log('sessionAuthenticationFailed');
            this.set('errorMessage', message);
        },

        // handle login success
        sessionAuthenticationSucceeded: function() {
          console.log('sessionAuthenticationSucceeded');

            this.set('errorMessage', "");
            this.set('identification', "");
            this.set('password', "");
            this._super();
        }
    }
});

export
default LoginController;

到目前为止一切顺利,我可以通过登录表单对用户进行身份验证。但是当我按F5时,我必须重新登录。 LocalStorage 适配器为空。所以问题是我需要什么来持久化 token 和 session ?

注意:我无法更新到 ember-simple-auth 0.1.2,bower 找不到新版本。好像是https://github.com/simplabs/ember-simple-auth-component的github版本不是最新的。

编辑: 我已更新我的代码如下:

app.js

// init Ember.SimpleAuth
App.initializer({
    name: 'authentication',
    initialize: function(container, application) {
        Ember.SimpleAuth.Authenticators.OAuth2.reopen({
            serverTokenEndpoint: 'http://customdomain/access_token'
        });

        Ember.SimpleAuth.setup(container, application, { // @todo at version 0.1.2 of Ember-simple-auth, add container
            crossOriginWhitelist: ['http://customdomain'], // @todo remove when live
            // store: Ember.SimpleAuth.Stores.LocalStorage,
            authenticationRoute: 'article.login'
        });
    }
});

export default App;

登录 Controller :

var LoginController = Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMixin, {
    // authenticator: CustomAuthenticator, // not needed anymore

    actions: {
        // display an error when logging in fails
        sessionAuthenticationFailed: function(message) {
            this.set('errorMessage', message);
        },

        // handle login success
        sessionAuthenticationSucceeded: function() {
            this.set('errorMessage', "");
            this.set('identification', "");
            this.set('password', "");
            this._super();
        }
    }
});

export default LoginController;

最佳答案

我之前没有使用过 oauth2 身份验证器(只是为我编写的后端定制的身份验证器),但我认为应该应用相同的概念。

当您刷新页面时,ember-simple-auth 会调用您正在使用的 oauth2 身份验证器的 restore 方法。 restore 方法正在查找名为“access_token”的属性,以确认用户已通过您的服务器进行身份验证。当您使用 http://customdomain/access_token/ 处的端点进行身份验证时,您的 REST API 是否返回名为 access_token 的属性?如果没有,您需要确保这种情况发生,否则您将遇到刷新问题。这是 ember-simple auth 提供的 oauth2 身份验证器中的恢复方法:

restore: function(properties) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      // It looks for the 'access_token' property here which should have been set
      // by the authenticate method if you returned it from your REST API
      if (!Ember.isEmpty(properties.access_token)) { 
        _this.scheduleAccessTokenRefresh(properties.expires_in, 
                      properties.expires_at, 
                      properties.refresh_token);
        resolve(properties);
      } else {
        reject();
      }
    });
  }

此外,我认为在您的 sessionAuthenticationSucceeded 操作中您需要返回 true。否则,该操作不会传播到 ember-simple-auth ApplicationRouteMixin (除非您不使用该 mixin 或者不依赖于其 sessionAuthenticationSucceeded 方法,在这种情况下它并不重要)。

关于authentication - Ember.js Ember Simple Auth 在 LocalStorage 中保留身份验证信息不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22020545/

相关文章:

javascript - 错误子状态

javascript - Ember 中的单向绑定(bind)输入?

javascript - 集成组件中的 Emberjs 链接到 href 测试

ember.js - 无法使用属性绑定(bind)让静态属性在 emberjs 类中工作

ios - Swift:通过 RESTful API 进行身份验证后更改 ViewController

javascript - 尝试在 NodeJS 上使用 PFUser

node.js - NodeJS 快速 session req.session 未定义

javascript - 我应该将 token 存储在客户端的什么位置

java - 管理身份验证 token 的最佳实践

events - 模型更改时的 emberjs 事件