gpt4 book ai didi

javascript - Iron-Router 登录和注册最佳实践

转载 作者:行者123 更新时间:2023-11-30 16:12:22 26 4
gpt4 key购买 nike

对于我的 Iron-Router 登出重定向代码的一些反馈,我将不胜感激。

我打算只允许登录用户访问登录或注册以外的路径,并重定向其他用户以使用错误代码登录。

// routes.js with Router.beforeAction
if (!Meteor.userId()) {
// Get current route
var currentRoute = Router.current().route.getName();
// if the current user is not trying to login or register send to login with error
if (currentRoute = 'register'){
this.layout('login');
this.render('registerview');
} else if (currentRoute = 'login') {
this.layout('login');
this.render('loginview');
} else {
Session.set("errorMessage", "You need to log in to access this page.");
this.redirect('/login');
}
} else {
// otherwise don't hold up the rest of hooks or our route/action function
// from running
this.next();
}

这行得通,但我希望得到一些关于效率和良好代码的反馈。

我的 routes.js 文件也在/client 中,我知道登录逻辑应该发送到服务器方法,但是路由文件是否存在任何安全问题?

最佳答案

从逻辑上来说,我可以说是可以的。但我会用另一种方式来做: 我通常在 onBeforeAction() Hook 中进行所有测试。我使用函数进行验证,如果所有验证都通过,我调用 this.next()。如果出现错误,路由器将重定向(使用 Router.go())到未注册用户可以看到的路由。关于文件夹,我放置了 路由器。 jsroot/lib 文件夹中。 示例:

Router.onBeforeAction(mustBeSignedIn, {only:  [the templates you want to check]});

关于javascript - Iron-Router 登录和注册最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36045565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com