在 Meteor 的 FlowRouter 中检查登录

Check login in FlowRouter in Meteor

本文关键字:检查 登录 FlowRouter Meteor      更新时间:2023-09-26

我在 Meteor 中使用 FlowRouter。

在某些页面上,我想检查用户是否已登录,因此我做了一个触发器输入函数

const isAuthenticatedRedirect = ( context, redirect, stop ) => {
  if ( ! Meteor.loggingIn() && ! Meteor.userId() ) {
    FlowRouter.go( 'login' );
  }
};
const isAuthenticatedRoutes = FlowRouter.group( {
  name: 'isAuthenticated',
  triggersEnter: [ isAuthenticatedRedirect ]
} );

问题是它将用户重定向到/login,而不仅仅是显示登录模板。

我可以在所有页面上做一个if语句,然后在用户未登录的情况下显示登录名而不是真实页面,但我想直接在路由器中执行此操作会更聪明。

FlowRouter不允许

你渲染模板,因此重定向是唯一的选择。您需要安装另一个包以使其直接呈现,而不是使用 FlowRouter.go