Search code examples
meteorroutesiron-router

Redirect user to different route depending on registration process in Meteor using Iron Router


got quite a complicated problem.

In my application, there is a 5 step user registration/setup process.

  1. Create Account
  2. Choose an option
  3. Choose more options
  4. Create subscription with Stripe
  5. Connect Stripe account so application has read-only access to user's Stripe data

After these steps, the user has access to the dashboard.

What I want to do is make sure that

  1. If a user leaves/disconnects during a step when they log back in they will be redirected to the step they hadn't completed (currently my login submit handler redirects straight to dashboard, so I'm not sure how to handle this)
  2. Once a user has completed all steps if they were to navigate to the route of a step they won't be able to, it will just forward to dashboard

I'd really appreciate any help on how to do this. So far what I've done is create a field in the database attached to the user document which records what step they have completed last, and if they have completed all it stored "dashboard".

I'm using Iron Router and I think the answer may lie in onBeforeAction hooks but I'm not 100% sure.

Any help appreciated!


Solution

  • I actually managed to work this out so thought I'd post for anyone who has a similar issue.

    I extended the route controller for the routes I want to protect. The extended controller calls a meteor method that returns the value in the database that is stored when a user completes a registration step. I stored this value as a string which is the route the user should be redirected to.

    So now, when a user signs up and completes step 2 then logs out a few things happen.

    1. The app knows they have completed step 2 so stores the route for step 3 in a field on the user document (eg. Meteor.user().loginRedirectTo = '/subscribe')
    2. User comes back and logs in, the loginhandler has a onBeforeAction that calls the meteor method which returns the value from the database to the router of where the user should go next.
      1. Router sends users to that page.

    Here's the code for it

    Extended Controller:

    StageController = RouteController.extend({
      onBeforeAction: function() {
        Meteor.call('getUserRedirect', function(error, route) {
          if(error) {
            console.log(error);
          } else {
            Router.go(route);
          }
        });
        this.next();
      }
    });
    

    Meteor method to return route user needs to go to:

    Meteor.methods({
        'getUserRedirect': function () {
            if(!Meteor.userId()) { 
                 throw new Meteor.Error(401, "You aren't even logged in, stop calling methods!");
            }
            var user = Meteor.user();
            var redirectRoute = Meteor.user().loginRedirectTo;
            return redirectRoute;
        }
    });
    

    And finally assign that controller to each of the routes with this code:

    Router.route('/dashboard', {
      template: "dashboard",
      name: "dashboard",
      controller: "StageController"
    });