backbone.js - handling if a user is logged in or not

前端 未结 5 595
悲&欢浪女
悲&欢浪女 2020-12-04 05:29

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

5条回答
  •  时光取名叫无心
    2020-12-04 06:05

    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);
           }
         });
      }
    });
    

提交回复
热议问题