首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ember-Simple-Auth注销和登录

Ember-Simple-Auth注销和登录
EN

Stack Overflow用户
提问于 2013-11-12 21:11:05
回答 2查看 1.6K关注 0票数 3

我使用ember-simple-auth从express服务器获取令牌。从/api/account请求帐户详细信息并将其存储在会话中。

当用户注销时,ember data记录将被清除,帐户和accountId也将被清除。当下一个用户登录时,不会为该用户进行任何api调用,只会收集身份验证令牌。怎样才能打出这个电话?下面是我的设置:

代码语言:javascript
复制
Ember.Application.initializer({
  name: 'authentication',
  initialize: function(container, application) {

    //customize the session so that it handles the additional authenticated account
    Ember.SimpleAuth.Session.reopen({
      init: function() {
        this._super();
        //initializer the accountId from data potentially already present in a
        //session cookie (Ember.SimpleAuth.Session does this out of the box for authToken)
        var accountId = (document.cookie.match(/accountId=([^;]+)/) || [])[1];
        this.set('accountId', accountId);
      },
      setup: function(serverSession) {
        this._super(serverSession);
        console.log('+++' + serverSession.user_id);
        this.set('accountId', serverSession.user_id);
      },
      destroy: function() {
        this._super();
        var accountId = this.get('accountId');
        container.lookup('store:main').unloadAll('account');
        this.set('account', undefined);
        this.set('accountId', undefined);
      },
      accountIdChanged: function() {
        //save accountId in a session cookie so it survives a page reload (Ember.SimpleAuth.Session
        //does this out of the box for authToken)
        document.cookie = 'accountId=' + this.get('accountId');
      }.observes('accountId'),
      account: function() {
        var accountId = this.get('accountId');
        console.log('---' + accountId);
        if (!Ember.isEmpty(accountId)) {
          this.set('account', container.lookup('store:main').find('account', accountId));
          console.log('now');
        }
      }.property('accountId')
    });

    //set a custom session endpoint
    Ember.SimpleAuth.setup(container, application, {
      routeAfterLogin: 'main',
      routeAfterLogout: 'login',
      loginRoute: 'login',
      serverTokenEndpoint: '/token',
      autoRefreshToken: 'false'
    });
  }
});
EN

回答 2

Stack Overflow用户

发布于 2014-02-13 21:10:09

我在ember-simple-auth 0.11.1上遇到了同样的问题,最终将我的用户(或帐户)加载到了setup函数中。它在那里似乎工作得很好,并且避免了window.reload。

要使用您的代码(未测试):

代码语言:javascript
复制
setup: function(serverSession) {
    this._super(serverSession);
    console.log('+++' + serverSession.user_id);
    accountId = serverSession.user_id;
    if (!Ember.isEmpty(accountId)) {
      this.set('account', container.lookup('store:main').find('account', accountId));
      console.log('now');
    }
 }
票数 2
EN

Stack Overflow用户

发布于 2013-11-12 22:55:48

我想这是

代码语言:javascript
复制
document.cookie = 'accountId=' + this.get('accountId');

是一个问题,因为它将导致cookie被设置为

代码语言:javascript
复制
accountId=undefined

你会需要一些东西。如下所示:

代码语言:javascript
复制
document.cookie = 'accountId=' + (this.get('accountId') || '');

要真正清除它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19930108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档