我在 Angular 13 上,使用 MSAL 与 Azure AD 集成。我的问题是,一旦用户成功登录,登录组件就不会显示。但是当我刷新页面时,会显示登录组件。如何在用户登录后立即显示登录组件。
在实现调试时,在 AuthGaurd 启动时登录时,即使登录成功,this.msalsvc.instance.getActiveAccount() 也会返回 null
这是我的应用程序路由,
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth-guard.guard';
import { LoginComponent } from './components/login/login/login.component';
import { TestComponent } from './components/test/test.component';
import { RoleGuard } from './route-guard.guard';
const routes: Routes =[
{path: '', component: TestComponent, canActivate: [AuthGuard]}
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
这是我的 app.component.html
<app-header></app-header>
<div *ngIf="IsLoggedIn()">
<h3>Welcome {{userName}}</h3>
<h3>Your Assigned role: {{userRole}}</h3>
</div>
<div *ngIf="!IsLoggedIn()">
<h3>You are NOT logged in</h3>
</div>
<router-outlet></router-outlet>
<app-footer></app-footer>
这是 Auth Guard 的代码
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot, UrlTree } from '@angular/router';
import { MsalService } from '@azure/msal-angular';
import { map, Observable, Subscription } from 'rxjs';
import { CommonService } from './services/common-service.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate{
constructor(private msalsvc : MsalService){
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
console.log('Hi there, ');
if(this.msalsvc.instance.getActiveAccount() == null){
return false;
}
return true;
}
}
这是添加登录方法的常用服务
import { Injectable } from '@angular/core';
import { MsalService } from '@azure/msal-angular';
import { AuthenticationResult } from '@azure/msal-browser';
import { observable, Observable, Subject,of} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CommonService {
//private loggedIn : boolean = false;
private subject = new Subject<any>();
constructor(private authService: MsalService) {
console.info('Common service is created');
}
login() {
this.authService.loginPopup()
.subscribe((response: AuthenticationResult) => {
this.authService.instance.setActiveAccount(response.account);
this.updateLoginNotification(true);
});
}
isLoggedIn(): boolean {
return this.authService.instance.getActiveAccount() != null
}
isAsyncLoggedIn():Observable<boolean>{
return of(this.authService.instance.getActiveAccount() !=null);
}
onLoginCheck(): Observable<boolean>{
return this.subject.asObservable();
}
getName():any{
this.getRole();
return this.authService.instance.getActiveAccount()?.name;
}
getRole():any{
return (this.authService.instance.getActiveAccount()?.idTokenClaims as any).roles;
}
updateLoginNotification(item: any) {
this.subject.next(item);
}
}