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

347 Views Asked by At

I'm using facebook login for my app. I am able to complete login successfully but when i refresh the page session.isAuthenticate becomes undefined.

I think the logic to persist the session stays inside fetch method of torii-adapter. Below is my torii-adapter and please tell me what is missing.

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?)

3

There are 3 best solutions below

1
On

The Torii session variable should be set in the config file like this:

// 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: function() {
  return this.get('session').fetch().catch(function() {});
},
0
On

@Falke's answer was helpful but wasn't solving my issue.

after some r&d i found the session.isAuthenticated was coming as undefined because my fetch method wasn't being called. Thanks to @falke i was able to get it run.

I stored my token in localstorage instead of session using below code in open method of my adapter

    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

8
On
let token = session.token;

This going to be always undefined (session object is undefined). 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.