2016-03-16 30 views
1

我会感谢我的铁路由器注销重定向代码的一些反馈。铁路由器登录和注册最佳实践

我打算只允许登录用户访问路由其他比登录或注册,并重定向他人登录错误代码。

// 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文件在/客户端,我知道登录逻辑应发送到服务器方法,但是有任何安全问题的路由文件?

回答

2

就逻辑而言,我可以说没关系。但我会以另一种方式做到这一点: 我通常会在onBeforeAction()挂钩中进行所有测试。我使用函数进行验证,如果所有验证都通过,我将调用this.next()。如果发生错误,路由器将重定向(使用Router.go())到未注册用户可以看到的路由。 关于该文件夹,我将router. js放在root/lib文件夹中。 示例:

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

因此,反馈是:将测试定义为函数并以这种方式调用它们。我也在编辑答案。 – StefanL19

+0

shti go editna:D –

+0

喜欢这个?:http://codepen.io/SlartyBArtfast/pen/wGgGMZ – SlartyBartfast