[英]You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable
[英]Error You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable, call refreshSession in Cognito
我正在嘗試刷新 AWS Cognito 中用戶的訪問令牌。 我收到錯誤消息:您在預期 stream 的地方提供了“未定義”。 您可以在第一次調用方法 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);
}
})
);
}
在您的catchError
中, function 僅在else
子句中返回一個可觀察的( of(httpError)
)。 如果錯誤是 401,則沒有返回(即undefined )。即使您不關心返回值(在這種情況下, of(null)
或EMPTY
應該這樣做),也在那里返回一些東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.