admin管理员组文章数量:1290946
Using Angular 19 and angular-auth-oidc-client I have the HomeComponent code:
export class HomeComponent implements OnInit {
private readonly oidcSecurityService = inject(OidcSecurityService);
private readonly eventService = inject(PublicEventsService);
isAuthenticated = signal<boolean>(false);
isCheckingAuth = signal<boolean>(true);
ngOnInit(): void {
this.oidcSecurityService.isAuthenticated$.subscribe(({ isAuthenticated }) => {
this.isAuthenticated.set(isAuthenticated);
this.isCheckingAuth.set(false);
});
this.eventService
.registerForEvents()
.pipe(filter((event) => event.type === EventTypes.CheckSessionReceived || event.type === EventTypes.UserDataChanged))
.subscribe((event) => {
console.log('Auth event received:', event);
this.oidcSecurityService.isAuthenticated$.subscribe(({ isAuthenticated }) => {
this.isAuthenticated.set(isAuthenticated);
});
});
}
And the HTML is:
<div *ngIf="!isCheckingAuth(); else loading">
<div *ngIf="isAuthenticated(); else noAuth">
<button (click)="logout()">Logout</button>
<button (click)="logoffAndRevokeTokens()">Logout and revoke tokens</button>
Is Authenticated: {{ isAuthenticated() }}
</div>
</div>
<ng-template #noAuth>
<button (click)="login()">Login</button>
<hr />
</ng-template>
<ng-template #loading>
<p>Loading authentication...</p>
</ng-template>
After I login I still can see the login button for an instance.
And the same when I refresh the page and I am logged in.
How can I stop this flickering?
Using Angular 19 and angular-auth-oidc-client I have the HomeComponent code:
export class HomeComponent implements OnInit {
private readonly oidcSecurityService = inject(OidcSecurityService);
private readonly eventService = inject(PublicEventsService);
isAuthenticated = signal<boolean>(false);
isCheckingAuth = signal<boolean>(true);
ngOnInit(): void {
this.oidcSecurityService.isAuthenticated$.subscribe(({ isAuthenticated }) => {
this.isAuthenticated.set(isAuthenticated);
this.isCheckingAuth.set(false);
});
this.eventService
.registerForEvents()
.pipe(filter((event) => event.type === EventTypes.CheckSessionReceived || event.type === EventTypes.UserDataChanged))
.subscribe((event) => {
console.log('Auth event received:', event);
this.oidcSecurityService.isAuthenticated$.subscribe(({ isAuthenticated }) => {
this.isAuthenticated.set(isAuthenticated);
});
});
}
And the HTML is:
<div *ngIf="!isCheckingAuth(); else loading">
<div *ngIf="isAuthenticated(); else noAuth">
<button (click)="logout()">Logout</button>
<button (click)="logoffAndRevokeTokens()">Logout and revoke tokens</button>
Is Authenticated: {{ isAuthenticated() }}
</div>
</div>
<ng-template #noAuth>
<button (click)="login()">Login</button>
<hr />
</ng-template>
<ng-template #loading>
<p>Loading authentication...</p>
</ng-template>
After I login I still can see the login button for an instance.
And the same when I refresh the page and I am logged in.
How can I stop this flickering?
Share Improve this question asked Feb 13 at 16:47 Miguel MouraMiguel Moura 39.5k97 gold badges289 silver badges532 bronze badges1 Answer
Reset to default 1It might be due to SSR, just use defer()
so that the HTML is rendered only on the browser. Due to the HTML being rendered on the server and again hydrated on the browser, the flickering might be happening.
@defer() {
<div *ngIf="!isCheckingAuth(); else loading">
<div *ngIf="isAuthenticated(); else noAuth">
<button (click)="logout()">Logout</button>
<button (click)="logoffAndRevokeTokens()">Logout and revoke tokens</button>
Is Authenticated: {{ isAuthenticated() }}
</div>
</div>
}
You can also explore Hybrid Rendering with SSR routes
to conditionally render these routes.
// app.routes.server.ts
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{
path: '/protected-route', // This renders the "/protected-route" route on the client (CSR)
renderMode: RenderMode.Client,
},
本文标签: angularStop flickering of menu when is logged inStack Overflow
版权声明:本文标题:angular - Stop flickering of menu when is logged in - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515846a2382882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论