Understanding Can Activate guard in Angular
Guards in Angular play a crucial role in controlling access to different parts of an application. They are used to protect routes and can prevent users from navigating to certain pages without proper authentication or authorization.
One of the most commonly used guards in Angular is the CanActivate guard. This guard is used to determine whether a user can activate a particular route. It is often used to restrict access to certain parts of an application based on the user’s authentication status or role.
How to use CanActivate guard
When using the CanActivate guard, you can create a custom guard service that implements the CanActivate interface. This service can then be used to protect specific routes in your application by adding it to the route definition in your routing module.
Here’s an example of how to use the CanActivate guard in Angular:
“`typescript
import { Injectable } from ‘@angular/core’;
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from ‘@angular/router’;
import { Observable } from ‘rxjs’;
import { AuthService } from ‘./auth.service’;
@Injectable({
providedIn: ‘root’
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable
if (this.authService.isAuthenticated()) {
return true;
} else {
this.router.navigate([‘login’]);
return false;
}
}
}
“`
In this example, the AuthGuard service checks if the user is authenticated using an AuthService. If the user is authenticated, the canActivate method returns true, allowing the user to navigate to the protected route. If the user is not authenticated, the method returns false and redirects the user to the login page.
Using CanActivate guard in Angular 16
As of Angular 16, the CanActivate guard works in the same way as in previous versions of Angular. You can still create custom guard services and use them to protect routes in your application. However, it is always recommended to check the official Angular documentation for any updates or changes related to guards.
Overall, the CanActivate guard is a powerful tool that can enhance the security of your Angular application by controlling access to different routes. By properly implementing guards, you can ensure that only authorized users can access certain parts of your application, leading to a more secure and reliable user experience.
Muy bien explicado
Authguard is not working with browser back button. If you click on back button after logging in, it will allow the user to come back to login page again
sir when i added the canActivate:[authGuard] in the login component in the router the browser started loading what to do??
Great comparison between the class and fn guard. Thanks a lot!
sir tutorial to do this with lazy loading and admin routing module with 2 router outlet
One of the best i saw so far , thank you so much , very well explained
thank you sai
hello..it's useful but in angular16 how can i get the services because function not allow for constructor so i can't be able to use the service in function can you please help me?
Nice explanation, Thank you bro
bro pls make a video on vblack screen
brother this help me a lot .. but i love to follow ur angular content step by step there r so many angular playlists idk which one to start
Gracias!!
Thank you!👏
thanks a lot i have work 2maro i studied in this thank u so much
after login i cant able to redirect to login page again what to do for it
thanks, it's very
🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
very nice bro👍👍👍👍👍👍👍👍👍
Wow, I just found the fix in 7min <3<3
Ty so much!!! u are the best!! 🚀🚀
Nice one Brother it worked fine
how to use toast in my code. Thanks for your video
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
export const authGuard: CanActivateFn = () => {
//route, state
const router = inject(Router);
const infoAcc = sessionStorage.getItem('infoAcc');
if (infoAcc) {
const token = JSON.parse(infoAcc);
if (token?.accessToken) {
return true;
}
}
router.navigate(['/login']);
return false;
};