【发布时间】: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