Frage

In order to trigger an event that triggers before a route callback is executed, I use the following plugin:

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

This works well.
However, I want to redirect the user to a certain route (with trigger set to true) after checking if the user is currently authenticated in the back end.

I'm currently doing this with an Ajax request, but obviously this brings me trouble.
In the below code, the ajax callback will only get fired after the 'home' callback from the router was already executed (or any other route for that matter).
How can I get this done?

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

The 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 executing the callback, but in that case, the callback for the 'login' route will not get triggered either. This is not the purpose.

War es hilfreich?

Lösung

I found exactly the plugin that I required:

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

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

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(); 
            }
        } 
    })        
}; 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top