首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CanDeactivate中处理可观察对象

在CanDeactivate中处理可观察对象
EN

Stack Overflow用户
提问于 2019-07-03 15:40:24
回答 1查看 166关注 0票数 1

我有一个拦截器,它可以在令牌过期时捕获服务器错误。然后拦截器向api请求刷新的令牌。如果成功,拦截器将再次发送http请求。出错时,拦截器会删除本地存储,并应重定向到登录页面。

现在我有了一个带有canDeactivate守卫的页面。所以我想在canDeactivate保护中检查token是否有效。如果无效,我想向API请求一个刷新的令牌。在成功的时候,我不想离开页面。在错误时,我想注销用户,并重定向到登录页面。

代码语言:javascript
复制
// canDeactivateGuard
export interface CanComponentDeactivate {
  canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable({
  providedIn: 'root',
})
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
  canDeactivate(component: CanComponentDeactivate) {
    return component.canDeactivate ? component.canDeactivate() : true;
  }
}
代码语言:javascript
复制
// component
canDeactivate(): Observable<boolean> | boolean {
    if (!this.authService.isTokenValid()) {
      // here i want to ask the api for a new token
      // on error of api I want to redirect the user to login page
      // and don't want to show a modal
      return this.authService.refreshToken().pipe(
        map(res => {
          console.log('test');
          return false;
        }),
        catchError(err => {
          console.log('test error');
          return of(true);
        })
      );
    }
    if (Object.is(this.testForm.get('text').value, this.oldText)) {
      this.modalService.hide(1);
      return true;
    } else {
      this.modalRef = this.modalService.show(this.modalTemplate);
      return false;
    }
  }
代码语言:javascript
复制
// AuthService
 public refreshToken(): Observable<Token> {
    return this.http.post<{ data: any }>(`${environment.apiUrl}/auth/refresh`, {}).pipe(
      map(({ data }): any => {
        const apiToken = this.tokenAdapter.adapt(data);
        this.setJwtToken(apiToken);
        this.currentTokenSubject.next(apiToken);
        return apiToken;
      })
    );
  }

我不知道如何更改从api捕获错误的代码,以及如何重定向用户。未调用canDeactivate方法的map()和catchError()中的console.logs。

EN

回答 1

Stack Overflow用户

发布于 2019-07-03 16:51:02

refreshToken应该遵循下面的格式,这会很有帮助

代码语言:javascript
复制
    return this.http.get(`${environment.apiUrl}/auth/refresh`, {
    }).pipe(
      map(({ data }): any => {
        const apiToken = this.tokenAdapter.adapt(data);
        this.setJwtToken(apiToken);
        this.currentTokenSubject.next(apiToken);
        return apiToken;
      }),
      catchError(() => {
      return of(false);
    }));

然后在canDeactivate方法中

代码语言:javascript
复制
// component
canDeactivate(): Observable<boolean> | boolean {
  if(!this.authService.isTokenValid()) {
  // here i want to ask the api for a new token
  // on error of api I want to redirect the user to login page
  // and don't want to show a modal
  return this.authService.refreshToken().pipe(
    map(res => {
      if (!res) {
        console.log('test error');
        return true;
      }

      console.log('test');
      return false;
    })
  );
}
if (Object.is(this.testForm.get('text').value, this.oldText)) {
  this.modalService.hide(1);
  return true;
} else {
  this.modalRef = this.modalService.show(this.modalTemplate);
  return false;
}
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56865231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档