简体   繁体   English

为什么在使用torii时在ember中刷新后未定义会话服务

[英]Why session service does not defined after refresh in ember while using torii

I'm using facebook login for my app. 我正在为我的应用程序使用Facebook登录。 I am able to complete login successfully but when i refresh the page session.isAuthenticate becomes undefined. 我能够成功完成登录,但是当我刷新页面session.isAuthenticate时未定义。

I think the logic to persist the session stays inside fetch method of torii-adapter. 我认为保持会话的逻辑留在torii-adapter的访存方法中。 Below is my torii-adapter and please tell me what is missing. 以下是我的torii适配器,请告诉我所缺少的内容。

import Ember from 'ember';
import {createToken} from 'myapp-new/utils/myapp-utils';
//import User from 'myapp-new/user/model';

export default Ember.Object.extend({
  store: Ember.inject.service(),
  session: Ember.inject.service('session'),
  fetch(){
    let token = session.token;
    if(Ember.isEmpty(token)){
      throw new Error("No session found");
    }
    else{
      return Ember.RSVP.resolve({token});
    }
  },

  open: function(authentication){
    let authorizationCode = authentication.authorizationCode;
    let token = createToken();
    let adapter = this;
    return new Ember.RSVP.Promise(function(resolve, reject){
      console.log(authentication);
      Ember.$.ajax({
        url: 'http://myapp.com/getUserInfoWuthAuthCode.php',
       //url:'http://localhost:4200',
        data: { 'code': authorizationCode,token:token},
        success: Ember.run.bind(null, resolve),
        error: Ember.run.bind(null, reject)
      });
    }).then(function(data){
      let user = data.user[0];

      return {
        token:token,
        user: user
      };
    });
  }
});

UPDATE Below is code of my route 更新下面是我的路线代码

import Ember from 'ember';

export default Ember.Route.extend({
    session: Ember.inject.service('session'),
    actions:{
        signIn(){
            console.log("Working");
            this.get('session').open('google-oauth2').then(function(){
                console.log("Login Worked");
            }, function(error){
                console.log('error', 'Could not sign you in: '+error.message);
            });
        }
    },
    setupController(controller, model){
        controller.set('session', this.session);
    }
});

I'm not using any component hence hooking session inside route only. 我没有使用任何组件,因此仅将会话挂接到路由中。 One thing to note about is fetch method of my torii-adapter is not getting called at all (it should be called on refresh, isn't it?) 要注意的一件事是,我的torii-adapter的fetch方法根本没有被调用(应该在刷新时调用,不是吗?)

The Torii session variable should be set in the config file like this: 应该在配置文件中设置Torii会话变量,如下所示:

// config/environment.js
var ENV = {
  torii: {
  sessionServiceName: 'session'
}

This will inject the service variable to your routes among others. 这会将服务变量注入到您的路由中。 Also with each refresh of the app the session is closed, to avoid this use the beforeModel hook of your route, something like this: 同样,每次刷新应用程序时,会话都会关闭,为避免这种情况,请使用路由的beforeModel挂钩,如下所示:

beforeModel: function() {
  return this.get('session').fetch().catch(function() {});
},
let token = session.token;

This going to be always undefined ( session object is undefined). 这将始终是未定义的( session对象是未定义的)。 The right way to do is, 正确的做法是

let token = this.get('session.token')
// OR
this.get('session')['token']

I prefer the first option which looks more elegent, at least for me. 我更喜欢至少看起来更优雅的第一个选项。

@Falke's answer was helpful but wasn't solving my issue. @Falke的回答很有帮助,但并没有解决我的问题。

after some r&d i found the session.isAuthenticated was coming as undefined because my fetch method wasn't being called. 经过一些研发后,我发现session.isAuthenticated的状态未定义,因为未调用我的提取方法。 Thanks to @falke i was able to get it run. 多亏@falke,我才能够运行它。

I stored my token in localstorage instead of session using below code in open method of my adapter 我在适配器的open方法中使用以下代码将令牌存储在localstorage中,而不是会话中

    open: function(authentication){
    let authorizationCode = authentication.authorizationCode;
    //let token = createToken();
   // let adapter = this;
    return new Ember.RSVP.Promise(function(resolve, reject){
      console.log(authentication);   
      resolve({authorizationCode});
    }).then(function(data){      
      localStorage.setItem('token',authorizationCode);
      return {
        token:authorizationCode        
      };
    });
  }

I was able to find it back in my fetch method using code below 我可以使用下面的代码在我的提取方法中找到它

fetch(){
    let token = localStorage.getItem('token');
    console.log(token);
    if(Ember.isEmpty(token)){
      throw new Error("No session found");
    }
    else{
      return Ember.RSVP.resolve({token});
    }

Hope this will help 希望这会有所帮助

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

相关问题 Ember Torii Facebook身份验证无法获取用户电子邮件 - Ember Torii facebook authentication not able to get user email Ember-simple-auth,Torii和Facebook Oauth2的工作流程 - Workflow for Ember-simple-auth, Torii and Facebook Oauth2 页面刷新后Facebook会话始终为空-javascript - Facebook session always null after page refresh - javascript 强制Facebook会话刷新 - Force the Facebook Session to refresh 第一次单击“赞”按钮后,选项卡内容不刷新 - Tab Content Does Not Refresh After First Click of Like Button 为什么 FBAudienceNetwork 中的“(#2) 服务暂时不可用”? - Why does "(#2) Service Temporarily Unavailable" in FBAudienceNetwork? 在使用 devise 和 omniauth-facebook 时,为什么 facebook 显示“提供的应用 ID 看起来不像有效的应用 ID。”? - While using devise and omniauth-facebook, why does facebook show “The provided app ID does not look like a valid app ID.”? 为什么当我使用新的Facebook应用ID时,Facebook Album总是返回空白? - Why does Facebook Album always return empty while I using new facebook app id? 为什么使用XFBML时Fb:like不会在IE8上显示发送按钮? - Why Fb:like does not display the send button on IE8 while using XFBML? 刷新后,使用Facebook评论插件的应用评论消失 - App Comments using Facebook Comment Plugin disappear after refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM