错误您在预期流处提供了'undefined'。您可以在Cognito中提供一个Observable,Promise,Array或Iterable调用refreshSession

问题描述 投票:0回答:1

我正在尝试刷新AWS Cognito中用户的访问令牌。我收到错误消息:您在期望流的位置提供了'undefined'。您可以在第一次调用refreshSession方法之后立即提供Observable,Promise,Array或Iterable。我在Angular 7中使用HttpInterceptor。下面是我的代码:

public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // process the request
    return next.handle(request).pipe(
        catchError((httpError: any) => {
            // check if the error is a permission issue
            if (httpError instanceof HttpErrorResponse && httpError.status === 401 {
                const poolData = {
                    UserPoolId: <userPoolId>,
                    ClientId: <clientId>
                };
                const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
                const cognitoUser = userPool.getCurrentUser();
                if (cognitoUser != null) {
                    cognitoUser.getSession((error, session) => {
                        if (error) {
                            cognitoUser.signOut();
                            this.router.navigate(['/login']);
                        }
                        cognitoUser.refreshSession(session.refreshToken, (refreshError, refreshSession) => {
                            if (refreshError) {
                                cognitoUser.signOut();
                                this.router.navigate(['/login']);
                            }
                            // set the cognito credentials
                            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                                IdentityPoolId: this.identityPoolId,
                                Logins: { [`cognito-idp.${<region>}.amazonaws.com/${<userPoolId>}`]: refreshSession.getIdToken().getJwtToken() }
                            });
                            (AWS.config.credentials as AWS.Credentials).refresh(err => {
                                if (err) {
                                    cognitoUser.signOut();
                                    this.router.navigate(['/login']);
                                }
                            });
                        });
                    });
                }
            } else {
                return of(httpError);
            }
        })
    );
}
angular amazon-cognito access-token interceptor refresh-token
1个回答
1
投票

catchError内部,该函数仅在of(httpError)子句中返回一个可观察的(else)。如果错误是401,则不会有返回值(即undefined)。即使您不关心返回值,也要在此返回值(在这种情况下,of(null)EMPTY应该会)。

© www.soinside.com 2019 - 2024. All rights reserved.