Ole
Ole

Reputation: 47058

Hide Angular navigation controls if user is not logged in?

Does Angular have a standardized way of hiding controls if the user is not logged in. We have the CanActivate guard which checks to see whether a user can visit a route. If we hide the route to start with if the user is not logged in or not authorized to access the route?

For example suppose we have a link:

 <a routerLink="topsecret">Top secret link</a>

We would like to hide this link when the application is rendered, if the user is not logged in. If the user is logged in and is authorized to proceed to view the topsecret component then render the link.

Upvotes: 0

Views: 2063

Answers (1)

Hamidreza Kalantari
Hamidreza Kalantari

Reputation: 493

Let's assume that on your authentication service(instantiated as Auth), you have an attribute named isLoggedIn which determines login state.

you can achieve that simply by using ngIf:

<a routerLink="topsecret" *ngIf="Auth.isLoggedIn">Top secret link</a>

Upvotes: 2

Related Questions