Login 如何仅允许访问特定路线
我使用主干网定义了一组路由 例如:Login 如何仅允许访问特定路线,login,backbone.js,routes,restriction,Login,Backbone.js,Routes,Restriction,我使用主干网定义了一组路由 例如: Backbone.Routes.prefix = Onethingaday.Routers Backbone.Routes.map "!*splat": "HomeRouter" : "reroute" "": "NavbarRouter" : "index" "SidebarRouter" : "index" "HomeRouter" : "index" "all": "NavbarRouter" : "index"
Backbone.Routes.prefix = Onethingaday.Routers
Backbone.Routes.map
"!*splat":
"HomeRouter" : "reroute"
"":
"NavbarRouter" : "index"
"SidebarRouter" : "index"
"HomeRouter" : "index"
"all":
"NavbarRouter" : "index"
"SidebarRouter" : "index"
"HomeRouter" : "all"
"news":
"NavbarRouter" : "index"
"SidebarRouter" : "index"
"NotificationsRouter" : "index"
"popular/threads":
"NavbarRouter" : "index"
"SidebarRouter" : "index"
"DiscoverRouter" : "popularThreads"
现在,我想限制对某些路由的访问,即如果用户“isLoginedIn”,则只能访问“all”和“news”路由。我怎么能在这里结账?如果用户尝试访问“全部”和“新闻”页面(例如,取决于您存储当前用户状态的方式,无论是
SessionController
还是CurrentUser
型号),我是否可以检查、比较并重定向用户到单独的登录页面,您可以始终按照currentUser.isLoggedIn()
的方式公开方法
我不能完全确定您的映射,因为主干网只为路由公开了一个深度哈希映射(您是否使用其他方法来定义路由?)
在我的一个主干应用程序中,我有如下代码:
home: function homeFn()
{
if (this.app.currentUser.isAnonymous())
{
this.app.page('index');
}
else
{
this.app.page('dashboard');
}
}
这使您可以直接控制路线映射。如果您不喜欢这样,您可以随时扩展主干.Router对象,使其具有before/after过滤器。在撰写本文时,这是我以前使用过的方法,它帮助我很好地完成了您想要实现的目标。您好,感谢您的回复。是的,我已经有了一个方法来知道用户是否登录,所以我可以检查用户的isLoggedIn。我不确定的是如何区分“登录”用户和“未登录”用户的路由。例如,对于“新闻”路由,loggedin用户应该能够访问w/o问题,但对于未登录的用户,我希望将其重新定向到单独的登录页面。基本上,我打算有一些不同的行动,为不同的路线,如果用户没有登录,例如新闻->转到登录页面,流行-允许访问等