简体   繁体   English

角度等待订阅完成,然后返回

[英]angular wait for subscription finishes and then return

In my ionic app I check if user is authenticated or not using isAuthenticated method. 在我的离子应用程序中,我检查用户是否已通过isAuthenticated方法进行了身份验证。
in isAuthenticated() I check if there is a valid token in storage. isAuthenticated()我检查存储中是否有有效的令牌。
if access token is expired, I refresh the token and again I return the new access token. 如果访问令牌已过期,则刷新令牌,然后再次返回新的访问令牌。
my problem is when I try to refresh token using refreshToken method, app doesn't wait until it finishes so access token will be returned null. 我的问题是,当我尝试使用refreshToken方法刷新令牌时,应用程序不会等到完成后才将访问令牌返回为null。

export class AuthService {
    token: JwtToken;
    isAuthenticated(): Promise<boolean> {
        return this.getToken().then(token => {
            return token != null;
        });
    }

    getToken(): Promise<JwtToken> {
        return this.storage.get('token').then(async (token: JwtToken) => {
            if (token == null) {
                return null;
            }
            const jwtHelper = new JwtHelperService();
            if (jwtHelper.isTokenExpired(token.access_token)) {
                console.log('1');
                // I need wait until the function below finished
                await this.refreshToken(token);
                console.log('3');
                // and then return refreshed access token
                return this.token;
            } else {
                return token;
            }
        });
    }

    async refreshToken(token: JwtToken) {
        console.log('2-1');
        return await this.http.post('http://api.com/auth/refresh', {refresh_token: token.refresh_token}).subscribe(async (res: JwtToken) => {
            console.log('2-2');
            this.token = res;
            await this.setToken(res);
            console.log('2-3');
            return this.token;
        });
    }
}

this is console output: 这是控制台输出:

1
2-1
3
2-2
2-3

while I need it to be 虽然我需要它

1
2-1
2-2
2-3
3

it ignores await refreshToken(token) somehow... 它以某种方式忽略await refreshToken(token) ...

the solution is to use async,await like this: if you want to call a function refereshToken : let result= await refereshToken(argument) ; 解决方案是使用async,await,如下所示:如果要调用函数RefereshToken:let result = await RefereshToken(argument); and the function of refereshToken's prototype you should wirte like this: async refreshToken(token: JwtToken) { return this.http.post(' http://api.com/auth/refresh ', {refresh_token: token.refresh_token}).subscribe(async (res: JwtToken) => { this.token = res; await this.setToken(res); return this.token; }); 以及RefereshToken原型的功能,您应该像这样:async refreshToken(token:JwtToken){返回this.http.post(' http://api.com/auth/refresh ',{refresh_token:token.refresh_token})。 subscription(async(res:JwtToken)=> {this.token = res;等待this.setToken(res);返回this.token;}); } }

this question helped me to solve my problem. 这个问题帮助我解决了我的问题。

I changed refreshToken like this: 我像这样更改了refreshToken

refreshToken(token: JwtToken) {
    return new Promise(resolve => {
        this.http.post('http://api.com/auth/refresh', {refresh_token: token.refresh_token}).subscribe(async (res: JwtToken) => {
            this.token = res;
            await this.setToken(res);
            resolve();
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM