我正在尝试刷新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);
}
})
);
}
答案 0 :(得分:1)
在您的catchError
内,该函数仅在of(httpError)
子句中返回可观察的(else
)。如果错误是401,则没有返回值(即 undefined )。即使您不在乎返回值(在这种情况下,of(null)
或EMPTY
应该可以。)