Backbone.js的 - 如果处理一个用户登录或不 [英] backbone.js - handling if a user is logged in or not

查看:114
本文介绍了Backbone.js的 - 如果处理一个用户登录或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,应该说供应该应用的静态页面是登录页面?

Firstly, should the static page that is served for the app be the login page?

其次,我的服务器端code是罚款(它不会给任何数据,用户不应该能够看到)。但是,如何让我的应用程序知道,如果用户没有登录,回到登录表单?

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?

推荐答案

我有一个后端的呼叫,我的客户端code,我的静态页面(的index.php),使检查当前用户是否登录。比方说,你在 API /认证/ LOGGED_IN 后端调用返回的HTTP状态code 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆