ember-simple-auth + oauth2:会话在刷新时丢失

时间:2019-04-14 07:17:09

标签: oauth-2.0 ember-simple-auth

我读到会话还原应该可以立即使用,但不适用于我,而且我找不到我做错了什么。

authenticators / oauth2.js:

import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';

export default OAuth2PasswordGrant.extend({
    serverTokenEndpoint: '/oauth/token'
});

routes / application.js:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Route.extend(ApplicationRouteMixin, {
    currentUser: service(),

    beforeModel() {
        return this._loadCurrentUser();
    },

    sessionAuthenticated() {
        this._super(...arguments);
        this._loadCurrentUser();
    },

    _loadCurrentUser() {
        return this.get('currentUser').load().catch(() => this.get('session').invalidate());
    }
});

services / current-user.js:

import Service from '@ember/service';
import { inject as service } from '@ember/service';
import RSVP from 'rsvp';

export default Service.extend({
    session: service('session'),
    store: service(),

    load() {
        if (this.get('session.isAuthenticated')) {
            return this.get('store').queryRecord('user', { me: true }).then((user) => {
                this.set('user', user);
            });
        } else {
            return RSVP.resolve();
        }
    }
});

1 个答案:

答案 0 :(得分:0)

最简单的方法是实施restore方法,或者您只需使用自定义身份验证器覆盖restore()。希望对您有所帮助。

您也可以参考链接 https://github.com/simplabs/ember-simple-auth#implementing-a-custom-authenticator

这显示了如何制作这些选项。

我还将为您提供示例:

// app/authenticators/yours.js
restore() {
  return new RSVP.Promise(res, rej){
    return resolve(data);
  }
}

如果您已经登录,它将存储您的会话