Routing to home screen when not signed in

帅比萌擦擦* 提交于 2019-12-12 03:36:34

问题


I want to redirect all users to a home page if they are not logged in. Currently I am doing this:

Router.onBeforeAction(function() {
  this.render('loading');
  if (! Meteor.userId()) {
    this.render('Home');
  } else {
    this.next();
  }
});

Which I saw online, but this seems to be prevent my 'loading' template from ever showing and instead shows the default

Loading...

in the top left of the screen. If I change this.render('Home'); to this.redirect('/') I see the correct loading template. However in the '/' route I call another this.render('Home') which I think then triggers the onBeforeAction again which in turn will redirect to '/' which means you are now in an infinite loop of loading.

So what is the correct way to fix my Router.onBeforeAction to achieve my desried goal of making sure users that are not logged in can only see the Home template no matter what URL they type in and do this without messing up my custom loading template elsewhere on the site?


回答1:


You need to have two before hooks. One is for 'loading' and one is to check if the user is logged in.

The 'loading' one is linked up to your subscriptions and is usually a preinstalled hook with iron router.

Router.onBeforeAction(function() {
  if (! Meteor.userId()) {
    this.render('Home');
  } else {
    this.next();
  }
}, {except: ['route_one', 'route_two']});

Router.onBeforeAction('loading');

Your route:

Router.route('/', {
    name: 'home',
    subscriptions: function() { 
        //Array of Meteor.subscribe to wait for and display the loading template
        return [Meteor.subscribe("a_subscription")]
    },
    loadingTemplate: 'loading',
    action: function() {
        this.render("Home_logged_in");
    }
});

So here, if Meteor.subscribe("a_subscription") is loading it will display the loading template. If the user is logged in it will display the Home_logged_in template and if not logged in (on any route) the Home template.

You can also set exceptions using the names 'route_one' and 'route_two' for your templates (which you can rename to anything else). I named your home template home. But if you added home to the except list the onBeforeAction would not apply to home.




回答2:


I use this onBeforeAction hook which redirect to the home page if the user is not logged in:

var userAuthHook = function() {
  if (Meteor.userId()) {
    //continue to the requested url
    this.next();
  } else {
    //redirect to home
    this.redirect('/');
  }
};

Router.onBeforeAction(userAuthHook, {
  except: ['home']
});

Router.route('/', {
  name: 'home'
});

Note that I exclude the home page from the hook in order to avoid redirect loops and that the / route is named "home" to be referred to in the hook exclusions.

This works for me and also allows to use loading templates.



来源:https://stackoverflow.com/questions/30522609/routing-to-home-screen-when-not-signed-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!