简体   繁体   English

刷新后Torii会话不持久

[英]Torii session does not persist after refresh

Currently, my Ember-cli application logs out after refresh. 当前,我的Ember-cli应用程序刷新后注销。 I have altered my code a lot to try to get it to work, but none helped. 我已对代码进行了大量修改,以尝试使其正常工作,但没有任何帮助。 If necessary, I will try to implement authentication with another provider. 如有必要,我将尝试与其他提供程序进行身份验证。

I have a route for application, which is just the logout and a route for login, which deals with login, because the client wants to style it. 我有一条申请路线,这只是注销,还有一条登录路线,它处理登录,因为客户端要为其设置样式。

My config/environment.js file looks like this: 我的config / environment.js文件如下所示:

firebase: 'https://<my-website-here>.firebaseio.com/',
    torii: {
      sessionServiceName: 'session'
    },

app/adapters/application.js: 应用程序/适配器/ application.js中:

import Ember from 'ember';
import FirebaseAdapter from 'emberfire/adapters/firebase';

const { inject } = Ember;

export default FirebaseAdapter.extend({
  firebase: inject.service(),
});

app/torii-adapters/application.js: 应用程序/牌坊的适配器/ application.js中:

import Ember from 'ember';
import ToriiFirebaseAdapter from 'emberfire/torii-adapters/firebase';

export default ToriiFirebaseAdapter.extend({
  firebase: Ember.inject.service()
});

app/routes/application.js: 应用程序/路由/ application.js中:

import Ember from 'ember';

export default Ember.Route.extend({
 actions: {
   logout: function() {
       this.get('session').close().then(function() {
           this.transitionTo('application');
       }.bind(this));
   }
  }
});

app/routes/login.js: 应用程序/路由/ login.js:

import Ember from 'ember';

export default Ember.Route.extend({
  actions: {
    login: function() {
      var controller = this.get('controller');
      var email = controller.get('userEmail');
      var password = controller.get('userPassword');
        this.get('session').open('firebase', {
             provider: 'password',
             email: email,
             password: password
        }).then(function() {
            this.transitionTo('dashboard');
        }.bind(this));
    }
  }
});

Yeah, currently, login and logout works just fine, but I cannot refresh the page in the middle of the session, or else it logs out me automatically. 是的,当前,登录和注销工作正常,但是我无法在会话中间刷新页面,否则它将自动注销我。

Thanks to anyone in advance. 预先感谢任何人。

Torii does not persist your authentication session for you. Torii不会为您保留身份验证会话。 You need to implement this yourself in the open , fetch , and close hooks of your adapter. 您需要在适配器的openfetchclose钩子中自己实现。

The easiest way to do this is with the localStorage API . 最简单的方法是使用localStorage API

Fetch

In the fetch hook, you need to pull your session data out of localStorage. fetch挂钩中,您需要将会话数据从localStorage中拉出。 If there's nothing there, either throw an exception or return a promise which will reject . 如果那里什么也没有,请抛出异常或返回将被reject

Open 打开

Your adapter needs to resolve with the session data, but should also store it in localStorage for later sessions. 您的适配器需要使用会话数据进行resolve ,但也应将其存储在localStorage中以供以后的会话使用。

Close

Clear the localStorage key for your session data and return a promise which will resolve . 清除会话数据的localStorage密钥,然后返回将resolve

Example

Here's the application adapter from one of my applications , which uses the Slack API with Torii. 这是我的一个应用程序中的应用程序适配器,该适配器使用带有Torii的Slack API。 The open hook is likely different from what you need for Firebase, but hopefully this gives you something to work with. open钩子可能与Firebase所需的钩子不同,但是希望这可以为您提供一些帮助。

export default Ember.Object.extend({
  storage: Ember.inject.service(),

  fetch() {
    let token = this.get('storage.token');

    if (Ember.isEmpty(token)) {
      throw new Error('No token in storage');
    }

    return Ember.RSVP.resolve({ token });
  },

  open(authentication) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      Ember.$.ajax({
        type: 'POST',
        url: '/api/tokens',
        data: authentication,
        dataType: 'json',
        success: Ember.run.bind(null, resolve),
        failure: Ember.run.bind(null, reject)
      });
    }).then(data => {
      let token = data.accessToken;

      this.set('storage.token', token);

      return { token };
    });
  },

  close() {
    this.set('storage.token', null);

    return Ember.RSVP.resolve();
  }
});

My storage service is a wrapper around the browser localStorage API . 我的storage服务是浏览器localStorage API的包装

Here's a much easier way. 这是一个简单得多的方法。

app/routes/application.js: 应用程序/路由/ application.js中:

import Ember from 'ember';

export default Ember.Route.extend({
  beforeModel: function() {
    return this.get('session').fetch().catch(function() {});
  },

  model() {
    if(this.get('session.isAuthenticated')) {
      return this.store.findAll('post');
    } else {
      this.transitionTo('login');
  }
});

The key is the beforeModel fetching the session. 关键是beforeModel获取会话。

I found this little nugget here: https://blog.isnorcreative.com/2016/07/30/ember-firebase.html 我在这里找到了这个小金块: https : //blog.isnorcreative.com/2016/07/30/ember-firebase.html

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

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