vendredi 19 octobre 2018

Ember simple auth session Expiring(Invalidating) while Reloading

/adapters/application.js

import DRFAdapter from './drf';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DRFAdapter.extend(DataAdapterMixin, {
    authorizer: 'authorizer:custom',
    namespace: 'api',

    pathForType(){
        return '';
    },
    urlForCreateRecord(){
        return 'http://localhost:8000/api/';
    }
});

my authenticator file is

/authenticators/jwt.js

import Ember from 'ember';  
import Base from 'ember-simple-auth/authenticators/base';  
import config from '../config/environment';

const { RSVP: { Promise }, $: { ajax }, run } = Ember;
export default Base.extend({  

  tokenEndpoint: `http://localhost:8000/auth`,

  restore(data) {
    return new Promise((resolve, reject) => {
      if (!Ember.isEmpty(data.token)) {
        resolve(data);
      } else {
        reject();
      }
    });
  },

  authenticate(creds) {
    const { identification, password } = creds;
    const data = JSON.stringify({
        email: identification,
        password: password
    });
    const requestOptions = {
      url: this.tokenEndpoint,
      type: 'POST',
      data,
      contentType: 'application/json',
      dataType: 'json'
    };
    return new Promise((resolve, reject) => {
      ajax(requestOptions).then((response) => {
        const { jwt } = response;
        // Wrapping aync operation in Ember.run
        run(() => {
          resolve({
            token: jwt
          });
        });
      }, (error) => {
        // Wrapping aync operation in Ember.run
        run(() => {
          reject(error);
        });
      });
    });
  },

  invalidate(data) {
    return Promise.resolve(data);
  }
});

login controller:

/login/controller.js

import Controller from '@ember/controller';
import { inject } from '@ember/service';

export default Controller.extend({
    session: inject('session'),
    actions: {

        authenticate: function(){

            let credentials = this.getProperties('identification','password');
            let authenticator = 'authenticator:jwt';
            this.get('session').authenticate(authenticator, credentials).catch(()=>{
                this.set('errorMessage','Login Failed');
            });
            this.set('userData',credentials.identification);

        }
    }
});


/application/controller.js


import Controller from '@ember/controller';
import { inject as service } from '@ember/service';

export default Controller.extend({
    session: service('session'),
    actions: {
        invalidateSession: function(){
            this.get('session').invalidate();
        }
    }
});

Environment.js file in the /config/ is

/config /environment.js
......
ENV['ember-simple-auth'] = {
    authorizer: 'authorizer:custom',
    // authenticationRoute: 'login',
    routeAfterAuthentication: '/profiles',
    //authorizationPrefix: 'JWT' - do not uncomment this line
    serverTokenEndpoint: '/auth',
  };
......

As you can see in the /config/environment.js file, after authentication the app will redirect to the route profile. and when I refresh the profiles route, the session invalidates automatically. I think the problem is that I haven't store-session(I'm not sure, and don't know how to do that). So please read the codes and help me to solve the issue




Aucun commentaire:

Enregistrer un commentaire