David Weldon
David Weldon

Reputation: 64312

Meteor: redirect a user if already logged in

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.

Upvotes: 5

Views: 4393

Answers (5)

Oliver Lloyd
Oliver Lloyd

Reputation: 5004

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.

Upvotes: 3

jameslopez
jameslopez

Reputation: 1

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'});

Upvotes: 0

Xiv
Xiv

Reputation: 10043

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}
)

Upvotes: 0

David Weldon
David Weldon

Reputation: 64312

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.

Upvotes: 0

TimDog
TimDog

Reputation: 8918

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');

Upvotes: 2

Related Questions