【问题标题】:Error You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable, call refreshSession in Cognito错误您在预期流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable,在 Cognito 中调用 refreshSession
【发布时间】:2020-02-28 03:42:45
【问题描述】:

我正在尝试刷新 AWS Cognito 中用户的访问令牌。我收到错误消息:您在预期流的位置提供了“未定义”。您可以在第一次调用 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】:

    在您的catchError 中,该函数仅在else 子句中返回一个可观察的(of(httpError))。如果错误是 401,则没有返回(即 undefined)。即使您不关心返回值(在这种情况下,of(null) 或 @987654325 @ 应该这样做)。

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2017-09-18
      相关资源
      最近更新 更多