Тёмный

CanActivate Route Guard - Angular (Tutorial #24) 

Nisha Singla
Подписаться 32 тыс.
Просмотров 37 тыс.
50% 1

CanActivate Route Guard in Angular - Tutorial no. 24
Angular provides lots of features and ready-to-use services which allow seamless development of awesome web application.
And protecting routes with Guards are one of the important and useful feature.
Use this link to share this video : Share this video: • CanActivate Route Guar...
In our previous videos we have already learnt about Routing, Nested Routing and Parameterised routing.
Nested Routing: • Nested Routes - Angula...
Router Parameters: • Route Parameters - Ang...
Routing: • Routing - Angular (Tut...
If you see our current project we can notice that we can navigate to any route but sometimes you need to control access to different parts of your app for various reasons. Some of which may be:
1) the user is not authorized or authenticate to navigate to the target component.
2) Maybe you should fetch some data before you display the target component.
3) You might want to save pending changes before leaving a component.
You might ask the user if it's OK to discard pending changes rather than save them.
You add guards to the route configuration to handle these scenarios.
Guards in angular are interfaces which can tell the router whether or not it should allow navigation to a requested route.
So If all guards return true, navigation will continue. If any guard returns false, navigation will be cancelled.
Guards return either true if the user can access a route or false if they can’t.
They can also return an Observable or Promise that later on resolves to a boolean in case the guard can’t answer the question straight away,
for example it might need to call an API. Angular will keep the user waiting until the guard returns true or false.
There are five different types of Guards:
CanActivate
Checks to see if a user can visit a route.
CanActivateChild
Checks to see if a user can visit a routes children.
CanDeactivate
Checks to see if a user can exit a route.
Resolve
Performs route data retrieval before route activation.
CanLoad
Checks to see if a user can route to a module that lazy loaded.
For a given route we can implement zero or any number of Guards.
CanActivate Route
This class implements the CanActivate interface by implementing the canActivate function.
When canActivate returns true, the user can activate the route. When canActivate returns false, the user cannot access the route.
In the above example, we allow access when the user is logged in.
canActivate can also be used to notify the user that they can't access that part of the application, or redirect them to the login page.
Guards are implemented as services that need to be provided so we typically create them as @Injectable classes.
Angular Tutorial Series:
• Introduction- Angular ...
Follow me on:
►RU-vid : / nishasingla
►Facebook : / angularjs4beginners
►LinkedIn : / nishasingla

Опубликовано:

 

8 май 2020

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 69   
@arvindkumarchaurasia8237
@arvindkumarchaurasia8237 Год назад
A very simple and straight forward way of explaining, well done ✌✌
@pavandixit442
@pavandixit442 3 года назад
I love the way You Explain
@NishaSingla
@NishaSingla 3 года назад
Yes, soon
@myvue4587
@myvue4587 4 года назад
Awesome video, after a long time........ still we are waiting for upcoming videos......thank you so much for helping angular developers by making videos
@NishaSingla
@NishaSingla 4 года назад
It's my pleasure
@TechAahar
@TechAahar 3 года назад
Really awesome and well explained , Thank you for sharing with us.
@rahulrahul6259
@rahulrahul6259 11 месяцев назад
A very simple and great way of explaining things
@priyankaravichandran851
@priyankaravichandran851 3 года назад
well explained , really awesome , can you please upload more about the routers , other more concepts , ES6 FEATURES
@konadeepak9443
@konadeepak9443 4 года назад
Hi Nisha, please try to make complete Angular topics and upload. The way of your presentation is very much understandable to every one. Thank You.
@NishaSingla
@NishaSingla 4 года назад
thanks. I will try to upload more topics
@bachubrahmam3811
@bachubrahmam3811 3 года назад
Thank you so much . It was clean and understabl.
@NishaSingla
@NishaSingla 2 года назад
You're welcome!
@NishaSingla
@NishaSingla 4 года назад
*Watch these videos as well* 👍 Routing: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-E8QOJ822Ta4.html Nested Routing: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-JfNLM0hq_uk.html Named Router Outlet: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ev6z6B3GVlw.html
@anshvarun653
@anshvarun653 3 года назад
Thank you!!! Saved my day :)
@NishaSingla
@NishaSingla 3 года назад
You're welcome!
@RZ009
@RZ009 3 года назад
More power to you !!!
@NishaSingla
@NishaSingla 3 года назад
😀
@boddulasrikanth6047
@boddulasrikanth6047 2 года назад
Thank you for clear explaination :)
@NishaSingla
@NishaSingla 2 года назад
You’re welcome
@lokeshnallapureddy7208
@lokeshnallapureddy7208 4 года назад
Thankyou soo much nisha ma'am for updating this video on my request soo early and I can't know wt can I say more than thankyou
@NishaSingla
@NishaSingla 4 года назад
My pleasure 😊 Keep Learning
@shraddhamarathe1444
@shraddhamarathe1444 12 дней назад
Awesome explanation. very crisp and clear. :) Thanks !! I just have one doubt that can we also pass parameters to guards ??
@nickoletan
@nickoletan 3 года назад
Thanks, you save my life
@NishaSingla
@NishaSingla 3 года назад
You’re welcome
@andreluizguimaraes3786
@andreluizguimaraes3786 3 года назад
Nisha, first of all, thanks for this video (I saw also the another one about the CanDeactivate, and it helped me a lot!) Please, I would like to know if the resource presented on this video prevents also to navigate to another URL that the user put manually on the browser, or mouse click on other short cuts? Thanks in advance. Best regards.
@Akshita_k
@Akshita_k 4 года назад
Thank you so much very helpful
@NishaSingla
@NishaSingla 4 года назад
You’re welcome 😊
@Stylehackswithpal
@Stylehackswithpal 2 года назад
Hey, Could you please make video on NgModules concept, Mock Interview Questions. It would be a great help. Because I am like fun of your teaching way and learning a lot from you. Thank you so much for videos.
@NishaSingla
@NishaSingla 2 года назад
Thanks Payal 😀😀
@mannumahto8160
@mannumahto8160 3 года назад
Great explanation
@NishaSingla
@NishaSingla 3 года назад
Thank u 🙏
@IqbalAhmed-qk5ix
@IqbalAhmed-qk5ix 3 года назад
Awesome superb Thank u so much mam
@NishaSingla
@NishaSingla 3 года назад
Thanks for liking
@shitalunde9528
@shitalunde9528 2 месяца назад
Hello, your video's are really helpful. Thank you so much for this great job. Can you please make series on ngRx please it's request And thanks in advance
@SalmanAli-gd3tm
@SalmanAli-gd3tm Год назад
Awesome video very helpful for me And I am waiting for realtime project. And when will you start realtime project any class or video ?
@jeckyanand717
@jeckyanand717 4 года назад
Thank you for this video. My angular learning was kind of stuck in middle because your angular playlist is very basic and incomplete. Pleaseease update this. People like me is waiting badly.
@NishaSingla
@NishaSingla 4 года назад
I will try my best to complete this series. Thanks for liking.
@Jacks7468
@Jacks7468 4 года назад
@@NishaSingla Please update the playlist of your angular tutorials with the latest version and changes
@31sar
@31sar 2 года назад
Please make videos for Angular projects
@Sharwankumar-rz4ft
@Sharwankumar-rz4ft 3 года назад
good work
@NishaSingla
@NishaSingla 2 года назад
Thank you! Cheers!
@christianfuchs9750
@christianfuchs9750 Год назад
Actually annotating a service with '@Injectable({providedIn: …})' @6:04 and declaring it in @NgModule({ providers: … }) @6:08 is redundant and may even become ambiguous; only when omitting the 'providedIn' you may need to declare it in 'providers'. Either works, but mixing it may lead to confusion and hard-to-solve DI problems when the DI hierarchy grows more complex.
@bharathgorental4928
@bharathgorental4928 2 года назад
this is awesome, but Can you please tell me If I want to restrict a user from a specific HTML element / typescript method/ function inside the component(some functions/ elements should be accessible but some should not), thank you.
@kalladhamodar4989
@kalladhamodar4989 2 года назад
Hii ur explantion is awesome,,, is services are compulsary link on providers(modules.ts)?
@kyleryxn
@kyleryxn 2 года назад
In which video of yours do you go over JWT tokens?
@manojnandhan8677
@manojnandhan8677 4 года назад
Thanks
@NishaSingla
@NishaSingla 4 года назад
Welcome
@abhinaygalipally9100
@abhinaygalipally9100 11 месяцев назад
Hi Nishna Singla can you expalin after sucessful authentication when am trying to hit /login,/register i should not navigate to login ore register can i know the solution
@kushalbaldev8490
@kushalbaldev8490 3 года назад
If we move back in browser after logout we are able to move back to previous page. PS I clear every data while one clicks logout.
@ankitg2361
@ankitg2361 3 года назад
CanActivate is not holding till http api response ...so could u help how do we hold till response
@surabhisahu1231
@surabhisahu1231 Год назад
Hello mam how can I pass user object to isLoggedIn() method as an argument? When I am doing that in auth.ts file it will showing some error please help me maam
@muralimanohar2514
@muralimanohar2514 4 года назад
please make more tutorials
@_falgunip
@_falgunip 3 месяца назад
what if we hit localhost:4200/users directly from address bar?
@ABHISHEKKUMAR-ne3rk
@ABHISHEKKUMAR-ne3rk 4 года назад
Pls,make video on angular login with jwt token
@NishaSingla
@NishaSingla 4 года назад
Sure
@Sandeep-lb7wb
@Sandeep-lb7wb 4 года назад
Can I expect videos on all lifecycle hooks and router events
@NishaSingla
@NishaSingla 4 года назад
I will upload videos on these topics soon. Thanks
@mahavirsinhpadhiyar864
@mahavirsinhpadhiyar864 4 года назад
Can we hide the menu navigation using canActivate? I have used the same in my application but I have to refresh the page to see the effect after logout.
@NishaSingla
@NishaSingla 4 года назад
Can you please explain a bit your requirement, where exactly you need this ?
@mahavirsinhpadhiyar864
@mahavirsinhpadhiyar864 4 года назад
@@NishaSingla What I am asking is can we show and hide the Menu bar according to isActivate function so no need to show if it is not allow.
@NishaSingla
@NishaSingla 4 года назад
@@mahavirsinhpadhiyar864 Ideally Guards are meant to use to control route accessibility . But you can try by setting any flag (using any global state or localStorage) which can be set in canActivate Method so that Menu can toggle as per the value set in this function.
@mahavirsinhpadhiyar864
@mahavirsinhpadhiyar864 4 года назад
@@NishaSingla ok thank you so much
@NishaSingla
@NishaSingla 4 года назад
@@mahavirsinhpadhiyar864 You're welcome
@vishalpal8957
@vishalpal8957 2 года назад
i have two doubts i hope you will see this comment and reply soon 1: if you have already added injectable ({ providedIn: 'root' }) then it is not necassary to add it in the app module as per my knowledge then why did u do that? 2: what is the use of the properties which "canActivate" consists the Activatedroutesnapshot and the Routestatesnapshot. cause as far as i know they are also very important i do not know why yet but i read this somewhere.
@CristianRomero-up1nj
@CristianRomero-up1nj 3 года назад
put a example condition plz
@jayakumar2927
@jayakumar2927 3 года назад
Good one can you share the code
@pardhasaradhinaidu602
@pardhasaradhinaidu602 4 года назад
We want angular jwt authentication in deep
@NishaSingla
@NishaSingla 4 года назад
I will try to upload it soon. Thanks
Далее
CanActivateChild Route Guard - Angular (Tutorial #25)
10:56
CanDeactivate Route Guard - Angular (Tutorial #26)
14:32
@ViewChild/@ViewChildren in Angular (Tutorial 35)
19:31
Angular 18 is EXACTLY what we needed
9:15
Просмотров 62 тыс.
Promises vs Observables - Angular (Tutorial #30)
15:44
Resolve Route Guard - Angular (Tutorial #27)
8:47
Просмотров 19 тыс.