Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict unauthorised access in Component Angular 2

I have to prevent user to move on any route until user is logged in.

Can somebody help me how can we achieve this in Angular 2 Component Routing.

Thanks,

like image 859
Ashutosh Singh Avatar asked Dec 01 '25 22:12

Ashutosh Singh


1 Answers

You can use the @CanActivate decorator that angular2 provides. You decorate the component that you want to restrict with a condition. More info can be found in the angular docs:

https://angular.io/docs/ts/latest/guide/router-deprecated.html#!#lifecycle-hooks

It has an example using a similar decorator called @CanDeactivate. This is a good starting point, customize it to your needs.

like image 147
Omar Sourour Avatar answered Dec 03 '25 12:12

Omar Sourour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!