首页 > 解决方案 > 广播服务不适用于角度的“MSAL”

问题描述

我正在努力整合 MSAL 服务。我正面临广播服务的问题。

 this.broadcastService.subscribe('msal:loginSuccess', async () => {
      //this.checkoutAccount();
      console.info('msal:login');
      console.info(this.authService.getAccount);
      console.info(this.authService.getRedirectUri);
    });

我看到在本地存储中我可以看到令牌,但是当我尝试订阅上述代码时,它没有进入,也没有抛出任何错误。

我需要登录成功。

我也在 app.component.ts 中添加了这段代码

你能告诉我我做错了什么吗?

标签: angular

解决方案


我遇到过同样的问题。我有单独的服务来执行我放置广播服务订阅代码的身份验证。login-success 从未被解雇。事实证明,您只需在根组件(app.component)订阅广播服务。请检查您是否从多个地方订阅了它。

如果您需要将订阅代码放在 app.component 之外的单独服务中,您可以使用服务内部的订阅代码创建 initialize() 函数,并从 app.component init() 调用它。这对我有用。

AuthenticationServicet.ts:

initialize() {
  this._broadcastService.subscribe('msal:loginSuccess', (x) => {
    console.log("login success.");
    alert("success");
  });
}

app.component.ts:

ngOnInit() {
    this._authenticationService.initialize();
}

推荐阅读