Search code examples
javascriptmeteoriron-router

Meteor Session.set, Session.get


I am in need of some help. I configured the signup page for my app and it is working fine but now I want a user, upon verification, to be sent back to the exact url they were at before clicking signup. I have looked at the meteor docs and used session.set and session.get; they work but only for things insight the app. It seems once the user clicks the verification link I am unable to use session.get to go back to the exact webpage that was stored in session.set. The relevant code is below - any insight would be appreciated - thanks in advance!

Template.applyPageOne.events({
    'click .jsCandidateSignupRequest': function (event) {
        event.preventDefault();
        var applyUrlSet = window.location.href;
        Session.set('applyUrlSession', applyUrlSet);
        Router.go('signupCandidate');
    }
});

Router.map(function () {
    this.route('verifyEmail', {
        controller: 'AccountController',
        path: '/verify-email/:token',
        action: 'verifyEmail'
    });
    AccountController = RouteController.extend({
        verifyEmail: function () {
            Accounts.verifyEmail(this.params.token, function () {
                if(Roles.userIsInRole(Meteor.user(), ['candidate'])) {
                    var applyUrlGet = Session.get('applyUrlSession');
                    window.open(applyUrlGet,'_self', false);
                }else {
                    Router.go('dashboard');
                }
            });
        }
    });
});

Solution

  • You can not use Session in this case, because the value of Session is not shared between tabs of browser.

    I suggest using localStorage to store the link, like this:

     Template.applyPageOne.events({
      'click .jsCandidateSignupRequest': function(event) {
        event.preventDefault();
        var applyUrlSet = window.location.href;
        localStorage.setItem('applyUrlSession', applyUrlSet);
        Router.go('signupCandidate');
      }
    });
    
    Router.map(function() {
      this.route('verifyEmail', {
        controller: 'AccountController',
        path: '/verify-email/:token',
        action: 'verifyEmail'
      });
      AccountController = RouteController.extend({
        verifyEmail: function() {
          Accounts.verifyEmail(this.params.token, function() {
            if (Roles.userIsInRole(Meteor.user(), ['candidate'])) {
              var applyUrlGet = localStorage.getItem('applyUrlSession');
              localStorage.removeItem('applyUrlSession');
              window.open(applyUrlGet, '_self', false);
            } else {
              Router.go('dashboard');
            }
          });
        }
      });
    });