Auth Guard in Angular to prevent url visit

Hi Guys Today we are going to Learn about how to implement Auth Guard in angular Application and How you can apply

First we will create a service and main purpose of service is to provide or check that user is logged in or not in the application.

AuthService for check user is logged in or not

Now we are going to create for AuthGuard Service using CanActivate as mentioned in below image we will first inject AuthService in AuthGuard and check that is user is logged in or not .If User is logged in then we will return true otherwise we will return false and we will use router to navigate to login page.

AuthGuard Service

Now we are going to use AuthGuard Serivce inside app.routing.module.ts file as below in the mention image

Please let me know if it is useful for you

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akbar Sheikh

Software Developing is my hobby and want to learn more things everyday just like you