首页 > 解决方案 > 如何从角度应用程序中的反应应用程序接收更新的身份验证令牌

问题描述

我有一个角度应用程序,它与反应应用程序集成并在 UAT 中正常工作。我们面临的唯一问题是,react 为我们提供了一个仅有效一小时的令牌,一小时后过期,但 Angular 应用程序不知道令牌的有效性,我如何检查令牌是否仍然有效或在 Angular 中过期. 如果令牌已过期,那么如何从反应中获取新的有效令牌。

这是从 react 收到的内容:

@Input() data;
  @Output() callBackReactComponent: EventEmitter<any> = new EventEmitter<any>();
:
:
:
 ngOnInit(): void {
    console.log('data from react:');
    console.log(this.data);
    this.commonService.setToken(this.data);
}

我在我的 api 请求中使用这个令牌:

 apollo.create({
      link: setContext((_, { headers }) => ({
        headers: {
          ...headers,
          authorization: `Bearer ${commonSerivce.getToken()}`
        },
      })).concat(httpLink.create({ uri: `${baseUrl}/graphql` })),
      cache: new InMemoryCache(),
      defaultOptions: {
        watchQuery: { fetchPolicy: 'network-only', errorPolicy: 'all' },
        query: { fetchPolicy: 'no-cache', errorPolicy: 'all' },
      },
    });

在反应应用程序中,我将角度应用程序渲染为:

 <app-root ref={elem => this.nv = elem} data={this.props.token.split(" ")[1]}></app-root>

不涉及按钮或链接单击或表单,如何在不重新加载或刷新的情况下获取新令牌。

提前致谢。

标签: reactjsangular

解决方案


推荐阅读