mercredi 25 mai 2016

Emberjs - How to redirect to the last accessed route after session invalidated

We are using ember-simple-auth with cookie authentication and we want to redirect to the last accessed route after we login again when the cookie expires.

We manage to do the redirection for the following scenarios:

  1. Not authenticated and try to access a route from url
  2. Not authenticated and select an item from the navigation menu

Both, after successful authentication, we redirected to the requested route.

But, we want when our session cookie expired and the user tries to access a route to invalidate the session and redirect the user back to authentication page. When the user log in back we want to redirect him to the requested route.

For now we store the previous transition so we can do the redirection but after we invalidate the session the data are lost.

What is the best way to do this?

Our code looks like:

Custom Authenticator

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

export default Base.extend({
  restore() {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      let sessionCookie = window.Cookies.get('beaker.session.id');
      if(!window.isUndefined(sessionCookie)) {
        resolve(true);
      }else{
        reject();
      }
    });
  },
  authenticate(data) {
    return new Ember.RSVP.Promise(function (resolve, reject) {
      Ember.$.ajax({
        type: 'post',
        url: '/core/authentication/basic/login',
        data: data
      }).then((response) => {
        resolve({
          responseText: response
        });
      }, (error) => {
        reject(error);
      });
    });
  },
  invalidate() {
    return new Ember.RSVP.Promise(function (resolve, reject) {
      Ember.$.ajax({
        type: 'post',
        url: '/core/authentication/basic/logout'
      }).then(() => {
        resolve(true);
      }, () => {
        reject();
      });
   });
  }
});

Application Route:

import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

export default Ember.Route.extend(ApplicationRouteMixin, {
  session: Ember.inject.service('session'),
  beforeModel(transition) {
    if(!this.get('session.isAuthenticated') && transition.targetName !== 'core.authentication') {
      this.set('previousTransition', transition);
      this.transitionTo('core.authentication');
    }
  },
  actions: {
    willTransition(transition) {
      if (!this.get('session.isAuthenticated')) {
        this.set('previousTransition', transition);
      } else {
        let previousTransition = this.get('previousTransition');
        if (previousTransition) {
          this.set('previousTransition', null);
          previousTransition.retry();
        }
      }
    }
  }
 });

Authentication Route

import Ember from 'ember';

export default Ember.Route.extend({
  session: Ember.inject.service('session'),
  actions: {
    login() {
      let that = this;
      let { username, password } = this.controller.getProperties('username', 'password');
      let data = {username: username, password: password};

      if(this.get('session.isAuthenticated')) {
        this.get('session').invalidate();
      }

      this.get('session').authenticate('authenticator:basic', data).then(() => {
        let data = that.get('session.data.authenticated');
        // show response message
      }, (error) => {
        // show error
      });
     }
   }
});




Aucun commentaire:

Enregistrer un commentaire