Web
Analytics
angular authguard canactivate example | login in angular

For Consultation : +91 9887575540

Stay Connected :

angular  auth guard example

Routes enable the user to navigate through the application. Sometimes the user needs to do something before being allowed access to a certain part of the application—for example, log in. Route guards can be used to control access to certain routes.
There are two main types of route guards:
• CanActivate: Can the user navigate to a route? In this class, you can inject the router. This is useful to navigate the user a way to another resource if the user isn’t allowed to navigate to a route.
• CanDeactivate: Can the user move away from a route? Useful for prompting to save changes.

They are following ways of authentication

  • CanActivate
  • CanActivateChild
  • CanDeactivate
  • CanLoad
  • Resolve
Example

Write following code into app.module.ts




Step 2: Create two components login and dashboard.

Step 3: Create new guard using ng g guard auth

Step 4: create new service myservice.

Step 5: Set provides in app.module.ts




Output : 


Select your currency