Backbone.js - 处理用户是否登录 [英] backbone.js - handling if a user is logged in or not
问题描述
首先,为应用提供的静态页面应该是登录页面吗?
Firstly, should the static page that is served for the app be the login page?
其次,我的服务器端代码很好(它不会提供用户不应该看到的任何数据).但是如何让我的应用知道如果用户未登录,则返回到登录表单?
Secondly, my server side code is fine (it won't give any data that the user shouldn't be able to see). But how do I make my app know that if the user is not logged in, to go back to a login form?
推荐答案
我有一个后端调用,我的静态页面 (index.php) 使用客户端代码来检查当前用户是否已登录.假设您在 api/auth/logged_in
处有一个后端调用,如果用户已登录,则返回 HTTP 状态代码 200
否则返回 400
(使用 cookie基于会话):
I have a backend call that my client-side code that my static page (index.php) makes to check whether the current user is logged in. Let's say you have a backend call at api/auth/logged_in
which returns HTTP status code 200
if the user is logged in or 400
otherwise (using cookie-based sessions):
appController.checkUser(function(isLoggedIn){
if(!isLoggedIn) {
window.location.hash = "login";
}
Backbone.history.start();
});
...
window.AppController = Backbone.Controller.extend({
checkUser: function(callback) {
var that = this;
$.ajax("api/auth/logged_in", {
type: "GET",
dataType: "json",
success: function() {
return callback(true);
},
error: function() {
return callback(false);
}
});
}
});
这篇关于Backbone.js - 处理用户是否登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!