5

I'm using meteor-router, and I'd like to redirect a user to /user if he requests / and he is already logged in.

As expected, this just renders the user_index template rather than changing the URL:

Meteor.Router.add
  '/': -> if Meteor.userId() then 'user_index' else 'index'

I want to do something like this:

Meteor.Router.add
  '/': -> if Meteor.userId() then Meteor.Router.to '/user' else 'index'

update 6/4/14:

This question is no longer relevant, and iron-router should be used instead.

Community
  • 1
  • 1
David Weldon
  • 63,632
  • 11
  • 148
  • 146

5 Answers5

3

meteor-router is now deprecated. Instead use iron-router which can redirect based on logged in status using:

Router.configure({layoutTemplate: 'mainLayout'});

Router.map(function() {
  this.route('splash', {path: '/'});
  this.route('home');
});

var mustBeSignedIn = function(pause) {
  if (!(Meteor.user() || Meteor.loggingIn())) {
    Router.go('splash');
    pause();
  }
};

var goToDashboard = function(pause) {
  if (Meteor.user()) {
    Router.go('home');
    pause();
  }
};

Router.onBeforeAction(mustBeSignedIn, {except: ['splash']});
Router.onBeforeAction(goToDashboard, {only: ['splash']});

Example taken from: Meteor.js - Check logged in status before render

--OR--

Use the accounts-entry package. From their site:

Ensuring signed in users for routes

Use AccountsEntry.signInRequired(this) to require signed in users for a route. Stick that in your before hook function and it will redirect to sign in and stop any rendering. Accounts Entry also tracks where the user was trying to go and will route them back after sign in.

Community
  • 1
  • 1
Oliver Lloyd
  • 4,936
  • 7
  • 33
  • 55
2

You're looking for a filter -- here is a sample from the docs:

Meteor.Router.filters({
    'checkLoggedIn': function(page) {
        if (Meteor.loggingIn()) {
            return 'loading';
        } else if (Meteor.user()) {
            return page;
        } else {
            return 'signin';
        }
    }
});

// applies to all pages
Meteor.Router.filter('checkLoggedIn');
TimDog
  • 8,758
  • 5
  • 41
  • 50
  • Yeah I'm familiar with filters. This example only renders the `signin` template and does not change the URL. I've also tried adding a call to `Meteor.Router.to` in a filter and that didn't work either. – David Weldon Jan 14 '13 at 20:32
  • I haven't tried this yet, but because Router uses `pushState` to change the URL, you could possible [add it yourself](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) in the `filter`? – TimDog Jan 14 '13 at 20:50
0

According to this issue it looks like redirects are not part of meteor-router, and may not be. For now I ended up working around the issue. If the project changes to accommodate redirects I'll update my answer, or someone else can post another answer.

update 1/23/13:

I switched to using mini-pages, which correctly deals with this case and includes a lot of great functionality like layouts.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
0

Meteor Router lets you directly access the response object, so you can just do a 302 redirect. Something like the following will work:

Meteor.Router.add("/test/:_id", (id) ->
  this.response.writeHead '302', {'Location': '/blah/' + id}
)
Xiv
  • 8,862
  • 3
  • 27
  • 30
0

You can do this by using a standard filter and wrapping the redirect in a defer object.

Meteor.Router.filters({
requireLogin: function(page) {
    if(! (Meteor.loggingIn()|| Meteor.user()) ){
        Meteor.defer(function () {
            Meteor.Router.to('/login');
        });
    }
    return page;
}

Meteor.Router.filter('requireLogin', {except: 'login'});