Ошибка Вы указали «неопределенное» там, где ожидался поток. Вы можете предоставить Observable, Promise, Array или Iterable, вызывать refreshSession в Cognito. - PullRequest
0 голосов
/ 02 ноября 2019

Я пытаюсь обновить токен доступа пользователя в AWS Cognito. Я получаю сообщение об ошибке: Вы указали 'undefined', где ожидался поток. Вы можете предоставить Observable, Promise, Array или Iterable сразу после первого вызова метода refreshSession. Я использую HttpInterceptor в Angular 7. Ниже мой код:

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

1 Ответ

1 голос
/ 02 ноября 2019

Внутри вашего catchError функция возвращает наблюдаемое (of(httpError)) только в предложении else. Нет возврата (т. Е. undefined ), если ошибка 401. Также верните что-нибудь туда, даже если вас не волнует возвращаемое значение (в таком случае, of(null) или EMPTYдолжен сделать).

...