Backbone Router: Authentication Redirection

To trigger an event that fires before the route callback is executed, I use the following plugin:

https://github.com/boazsender/backbone.routefilter

It works well.
However, I want to redirect the user to a specific route (with the trigger set to true) after checking if the user is checked on the rear panel.

I am currently doing this with an Ajax request, but obviously this is causing me problems.
In the code below, the ajax callback will only start after the home router callback has already been completed (or any other route, for that matter).
How can i do this?

I have the following code:

var Router = Backbone.Router.extend({ 

    /* --- 1. Route management --- */ 

    routes: { 
        '': 'landing_page', 
        '(/)login': 'login', 
        '(/)home': 'home', 
        '*actions': 'defaultAction', 
        }, 
    before: function(route, params) { 
        var getAuthStatus = APP.controllers.auth_controller.isLogged(); 
        var self = this; 

        $.when(getAuthStatus).then(function(response){ 
            var auth_status = Object.keys(response.success)[0]; 
            if(auth_status === 'guest'){ 
                console.log(auth_status); 
                self.navigate("login", {trigger: true}); 
            } 
        }); 
    }
}); 

Ajax request:

Auth_controller.prototype.isLogged = function(){ 
    //Check if the user is authenticated 
    var getAuthStatus = this.auth_model.fetch(); 
    return getAuthStatus; 
}; 

Edit: "return false" in the "before" method will stop the callback, but in this case the callback for the "login" route will also not start. This is not a goal.

+4
source share
1 answer

I found exactly the plugin that I need:

https://github.com/fantactuka/backbone-route-filter

This plugin allows you to create / receive an Ajax request / response before the route is deleted.

In the router:

    routes: { 
        '': 'landing_page', 
        '(/)login': 'login', 
        '(/)home': 'home', 
        }, 
    /*--- Before filter checks authentication (BB async router plugin) ---*/
    before: { 
        '(/)login': 'redirect_auth', 
        '(/)home': 'redirect_auth', 
        '(/)users/create_role': 'redirect_auth' 
    }, 
    /*--- Checks if the user is logged in and redirects ---*/
    redirect_auth: function(fragment, args, next) { 
        APP.controllers.auth_controller.redirect(fragment, args, next); 
    }

The redirection logic is implemented as such in the auth_controller object:

/*--- Check if the user is logged ---*/ 
/*--- PHP REST Show (GET) ---*/ 
Auth_controller.prototype.isLogged = function(){ 
    //Check if the user is authenticated 
    var getAuthStatus = this.auth_model.fetch(); 
    return getAuthStatus; 
}; 

/*--- Redirect the user based on auth status (before -> route) ---*/
Auth_controller.prototype.redirect = function(fragment, args, next){ 
    var getAuthStatus = this.isLogged(); 
    var self = this; 

    $.when(getAuthStatus).then(function(response){ 
        var auth_status = Object.keys(response.success)[0]; 
        if(auth_status === 'guest'){ 
            if(fragment === 'login'){
                next(); 
            }else{ 
                APP.router.navigate("login", {trigger: true});                         
            } 
        } else { 
            var sidebars_controller = new Sidebars_controller(); 

            APP.user = response.success["auth"]; 
            if(fragment === 'login'){ 
                APP.router.navigate("home", {trigger: true});                         
            }else{ 
                next(); 
            }
        } 
    })        
}; 
+4
source

Source: https://habr.com/ru/post/1535621/


All Articles