Функции OnInit не загружаются при обновлении токена - PullRequest
0 голосов
/ 30 октября 2019

Это проект angularjs. Он работает просто отлично, но когда я нажимаю клавишу F5 для перезагрузки приложения, если ему нужно создать новый токен, функция loadgrid не вызывается.

Служебная часть работает просто отлично. Я думаю, что проблема связана со временем ожидания.

компонент

async ngOnInit() {

    const that = this;

    await this.gloService.refreshToken().then(function (result) {
            that.loginService.getRegrasUtilizador(result, IdRegra).then(function (resultado) {
            // when new token is created the grid not loads
            that.loadGrid();
            }
          }
    }

сервис

async refreshToken(): Promise<any> {


    this.url = localStorage.getItem('UrlServicoIdentidade');
    this.url = this.url.replace(/"/g, '');
    var token: any = JWT(localStorage.getItem("access_token"));

    // token is valid
    if ((parseInt(token.exp) - 10) > ((new Date).getTime() / 1000)) {
        return localStorage.getItem("access_token");
    }
    // create new token
    var token: any = JWT(localStorage.getItem("refresh_token"));
    if ((parseInt(token.exp) - 10) > ((new Date).getTime() / 1000)) {

        var urlAtual = this.url;
        urlAtual = urlAtual + "connect/token";
        var params: any = {};
        params.client_id = "portal";
        params.grant_type = "refresh_token";
        params.refresh_token = localStorage.getItem("refresh_token");
        var self = this;

            await this.http.post(urlAtual, params).toPromise()
                .then(function (rest: Response) {
                    var x = rest.json()
                    self.identidade = x;
                    //console.log("original token: " + JSON.stringify(JWT(localStorage.getItem("access_token"))));
                    localStorage.removeItem("access_token");
                    localStorage.removeItem("refresh_token");
                    localStorage.setItem("access_token", self.identidade.access_token);
                    localStorage.setItem("refresh_token", self.identidade.refresh_token);
                    console.log(localStorage.getItem("access_token"));
                    return  localStorage.getItem("access_token");
                })
                .catch(function (e)     { 
                    self.router.navigate(['login']);
                    console.log(e);
                    return null;
                })
    }
    else {
        this.router.navigate(['login']);
    }
}
...