Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after Login using Meteor and Iron Router

Tags:

I'm using the built in loginButtons options with Meteor and I would like to redirect after a user logs in. Using the built in web snippets means I can't use the callback with Meteor.loginwithPassword and I can't see any hooks inside Iron-Router to do the redirect.

Any suggestions?

like image 637
user2243825 Avatar asked Jan 23 '14 17:01

user2243825


2 Answers

Meteor often renders so quickly that the page is being loaded before the user has been defined. You need to use Meteor.loggingIn() to account for the situation in which you are in the process of logging in. This code works for me:

this.route('myAccount', {   path: '/',   onBeforeAction: function () {     if (! Meteor.user()) {       if (!Meteor.loggingIn()) Router.go('login');     }   } } 
like image 138
Charlie Morris Avatar answered Oct 03 '22 06:10

Charlie Morris


This example might be useful

// main route render a template Router.route('/', function () {     this.render('main'); });  // render login template Router.route('/login', function () {     this.render('login'); });     // we want to be sure that the user is logging in // for all routes but login Router.onBeforeAction(function () {     if (!Meteor.user() && !Meteor.loggingIn()) {         this.redirect('/login');     } else {         // required by Iron to process the route handler         this.next();     } }, {     except: ['login'] });  // add here other routes  // catchall route Router.route('/(.*)', function () {     this.redirect('/catchallpage'); }); 
like image 30
Brice Avatar answered Oct 03 '22 05:10

Brice