Route guards in Angular

Mayur Palase
2 min readAug 31, 2022

--

What is route guard?

Route guards are the interfaces and the mechanism provided by angular to control the accessibility of the routes. “Also we can say that to protect the routes from an unauthorized access”.

Types of route guards:

There are 5 types of route guards provided by angular as below.

1. CanActivate

2. CanActivateChild

3. CanDeactivate

4. CanLoad

5. Resolve

How to implement route guard in angular?

1) CanActivate:

All route guards are an interfaces those we need to implement. While implementing first we need to add canActivate guard in routes as below.

Routes in app-routing.module.ts in angular.

As we have added canActivate guard in routes and now we need implement AlwaysAuthGuard service as below. The return type of canActivate is

Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree

For now we are using Boolean as return type,

Now if we try to load any of the mentioned route in browser then it will execute the code of canActivate before loading the route.

to be continued

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mayur Palase
Mayur Palase

No responses yet

Write a response