简体   繁体   English

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

[英]ember-simple-auth + oauth2 : session lost on refresh

I read that session restore is supposed to work out of the box, but it doesn't for me, and I can't find what I did wrong.我读到会话恢复应该是开箱即用的,但它不适合我,而且我找不到我做错了什么。

authenticators/oauth2.js :身份验证器/oauth2.js :

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

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

routes/application.js :路线/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 :服务/当前用户.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();
        }
    }
});

The simple way is to implement the restore method or you just overwrite restore() with your custom authenticator.简单的方法是实现restore方法,或者您只需使用自定义身份验证器覆盖restore() Hope that's helpful.希望这有帮助。

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

This show the options how you can make it.这显示了如何制作它的选项。

I will also provide you the example:我还将为您提供示例:

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

This will store your session if you have already login如果您已经登录,这将存储您的会话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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