未登录时路由到主屏幕 [英] Routing to home screen when not signed in

查看:63
本文介绍了未登录时路由到主屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果所有用户未登录,我想将其重定向到主页.目前,我正在这样做:

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

我在网上看到过,但这似乎阻止了我的'loading'模板无法显示,而是显示了默认

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

正在加载...

Loading...

屏幕左上方的

.如果将this.render('Home');更改为this.redirect('/'),我会看到正确的加载模板.但是,在'/'路由中,我调用了另一个this.render('Home'),我认为它随后会再次触发onBeforeAction,这又将重定向到'/',这意味着您现在处于无限加载循环中.

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.

那么修复我的Router.onBeforeAction来实现我期望的目标的正确方法是什么,即确保无论他们键入什么URL都不会登录的用户只能看到Home模板,并且这样做不会弄乱我在网站上其他位置的自定义加载模板?

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?

推荐答案

您需要先插入两个钩子.一种用于加载",一种用于检查用户是否已登录.

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

您的路线:

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

因此,在这里,如果正在加载Meteor.subscribe("a_subscription"),它将显示loading模板.如果用户已登录,它将显示Home_logged_in模板,如果未登录(在任何路径上),则将显示Home模板.

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.

您还可以为模板使用名称"route_one"和"route_two"(可以将其重命名为其他名称)来设置例外.我将您的家庭模板命名为home.但是,如果将home添加到except列表中,则onBeforeAction将不适用于home.

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.

这篇关于未登录时路由到主屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆