Working with state in Backbone.js & logging in a user

喜夏-厌秋 提交于 2019-12-12 09:54:49

问题


I need to check that a user have logged in and been authenticated before letting my him or her use my backbone.js-based application.

The user authentication is stored in a native backbone model as a property, which I check before starting my main router by invoking Backbone.history.start. This way - an unauthenticated user is sent to the login page directly.

Is this sufficient?

if (!myApp.state.loggedIn) {
    window.location.hash = "login"; // Set url to #login
}

Backbone.history.start();  // Start history as usual

回答1:


if access to your backend REST API is protected server side and it wouldn't allow to access any user data when i'd for example stop the execution of code from developer tools and edit model's data (like set the state.loggedIn to true and set some user's i'd like to steal data from ID as my own). You should probably have also cookie session stored and maybe CSRF protection in to prevent cross site request forgery.

But basically backend should redirect user to login page whenever user isn't really logged in or his session expired.




回答2:


You can also use backbone async before and after filters to solve such problems.

I made a lib to handle logins and session stuff for us.

https://github.com/chirag04/backbone-async-route-filter



来源:https://stackoverflow.com/questions/8957543/working-with-state-in-backbone-js-logging-in-a-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!