简体   繁体   中英

Ember-Simple-Auth logout and in

I am using ember-simple-auth to obtain a token from an express server. the account details are requested from the /api/account and stored in the session.

When the user logs out, the ember-data record is cleared and the account and accountId cleared. When the next user logs in there is no api call made for that user, just the authentication token collected. How can I get this call to be made? Here is my setup:

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'
    });
  }
});

I struck the same issue on ember-simple-auth 0.11.1 and ended up putting my user (or account) loading into the setup function. It seems to work well there and avoids a window.reload.

To use your code (not tested):

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');
    }
 }

I think this

document.cookie = 'accountId=' + this.get('accountId');

is a problem as it will result in the cookie to be set to

accountId=undefined

You'd need sth. like this:

document.cookie = 'accountId=' + (this.get('accountId') || '');

to really clear it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM