logout with tokens in angularjs

0

The problem is in the logout button (exit) of the app, because in google chrome I have to click twice to this to execute the logout successfully and in the other browsers with a click it is already executed.

I am using TOKENS for the sessions. The method executed when we click on the logout button is the one shown below which in turn calls another method removeAllTokens() that contains two methods save() and clear() :

The clear method removes all tokens from the list.

From what I see if I comment on the clear() method that is inside removeAllTokens() the logout is done correctly clicking only once on the logout button but if it is not commented it is necessary to click twice on google chrome I create the problem is in the clear() method that removes the tokens from an array that contains the tokens but I do not understand what the code of the clear() method does.

clear() {
        for (let i=this.tokens.length-1; i > -1; i--) {
            if (this.tokens[i]) {
                var token;
                if (this.tokens[i].isValid) {
                    token = angular.extend({}, this.tokens[i].token);
                }
                this.remove(this.tokens[i]); // remove the token
                if (token) {
                    oauthState.notifyInvalidToken(token);
                }
            }
        }

        this.tokens = [];

        for (let i =0; i<this.reservedSlots; i++) {
            this.tokens.push(undefined);
        }
    }

save(): boolean {
        return storage.set(config.name, this.tokens.filter((t) => t && t.isValid).map((t) => t.token ));
    }

removeAllTokens() {
   clear();
   save();
}

logout() {
        let token = this.token.rcs().token;
        this.token.removeAllTokens();

        let redirect = () => {
            oauthState.config.logoutRedirect && window.location.replace(oauthState.config.logoutRedirect);
        };

        let redirect1 = () => {
            alert("error");
        };

        this.$http
            .post('${oauthState.config.baseAuthUrl}/${oauthState.config.revokePath}', {token: token.accessToken}, {withCredentials: true})
            .then(redirect, redirect1);
    }

I know it is complicated to give a satisfactory answer with just this code that I show but I can not publish everything because it is a private work code.

At least I would like to understand what is the process that is used to logout using tokens since I do not understand the process well.

    
asked by Reynier Téllez 10.10.2016 в 10:35
source

1 answer

0

I would exchange it for this:

   logout() {
    var token = this.token.rcs().token; //aca estas copiando por referencia puesto que es un objecto lo que asignas, si deseas que sea una copia independiente usa angula.copy().

    let redirect = () => {
        this.token.removeAllTokens(); //Entiendo que token.accessToken es donde reside el token que deseas desautorizar
        oauthState.config.logoutRedirect && window.location.replace(oauthState.config.logoutRedirect);
    };

    let redirect1 = () => {
        alert("error");
    };

    this.$http
        .post('${oauthState.config.baseAuthUrl}/${oauthState.config.revokePath}', {token: token.accessToken}, {withCredentials: true})
        .then(redirect, redirect1);
}
    
answered by 11.10.2016 в 23:44