{"id":59900,"date":"2018-05-03T16:02:15","date_gmt":"2018-05-03T10:32:15","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=59900"},"modified":"2024-04-12T14:20:50","modified_gmt":"2024-04-12T08:50:50","slug":"angular-router-and-route-protection","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/","title":{"rendered":"Angular Router and Route  Protection"},"content":{"rendered":"<h1>Angular Router and Route Protection<\/h1>\n<p>Angular Router is an official Angular routing library, written and maintained by the Angular Core Team.<\/p>\n<p>It\u2019s a JavaScript router implementation that\u2019s designed to work with Angular and is packaged as @angular\/router.<\/p>\n<p>First of all, Angular Router takes care of the duties of a JavaScript router:<\/p>\n<p>it activates all required Angular components to compose a page when a user navigates to a certain URL<br \/>\nit lets users navigate from one page to another without page reload<br \/>\nit updates the browser\u2019s history so the user can use the back and forward buttons when navigating back and forth between pages.<\/p>\n<p>In addition, Angular Router allows us to:<\/p>\n<p>redirect a URL to another URL<br \/>\nresolve data before a page is displayed<br \/>\nrun scripts when a page is activated or deactivated<br \/>\nlazy load parts of our application.<\/p>\n<p>In this article, we\u2019ll learn how to set up and configure Angular Router, how to redirect a URL and how to use Angular Router to resolve todos from our back-end API.<\/p>\n<p>In the next article, we\u2019ll add authentication to our application and use the router to make sure some of the pages can only be accessed when the user is signed in.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Route Protection<\/strong><\/p>\n<p>Angular comes with a number of baked-in features which are tremendously helpful for handling authentication. I think my favorite is probably its HttpInterceptor interface, but right next to it would be route guards. Let\u2019s take a look at what Angular\u2019s route guards are and how to use them to help with authentication in your Angular apps.<\/p>\n<p><strong>What are Route Guards?<\/strong><\/p>\n<p>Angular\u2019s route guards are interfaces which can tell the router whether or not it should allow navigation to a requested route. They make this decision by looking for a true or false return value from a class which implements the given guard interface.<\/p>\n<p>There are five different types of guards and each of them is called in a particular sequence. The router\u2019s behavior is modified differently depending on which guard is used. The guards are:<\/p>\n<p>CanActivate: Check if a user has access<br \/>\nCanActivateChild: Check if a user has access to any of the child routes<br \/>\nCanDeactivate: Can a user leave a page? For example, they haven&#8217;t finished editing a post<br \/>\nResolve: Grab data before the route is instantiated<br \/>\nLazy loading feature modules.<br \/>\nCanLoad: Check to see if we can load the routes assets<\/p>\n<p><strong>CanActivate<\/strong><\/p>\n<p>Guards are implemented as services that need to be provided so we typically create them as @Injectable classes.<\/p>\n<p>Guards return either true if the user can access a route or false if they can\u2019t.<\/p>\n<p>They can also return an Observable or Promise that later on resolves to a boolean in case the guard can\u2019t 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.<\/p>\n<p>Lets create a simple CanActivate guard.<\/p>\n<p>First we need to import the CanActivate interface, like so:<\/p>\n<p>import {CanActivate} from &#8220;@angular\/router&#8221;;<\/p>\n<p>Then lets create an Injectable class called AlwaysAuthGuard which implements the canActivate function, like so:<\/p>\n<p>class AlwaysAuthGuard implements CanActivate {<br \/>\ncanActivate() {<br \/>\nconsole.log(&#8220;AlwaysAuthGuard&#8221;);<br \/>\nreturn true;<br \/>\n}<br \/>\n}<\/p>\n<p>This guard returns true all the time, so doesn\u2019t really guard anything. It lets all users through but at the same time our guard logs &#8220;AlwaysAuthGuard&#8221; to the console so we can at least see when it\u2019s being used.<\/p>\n<p>We need to provide this guard, for this example lets configure it via our NgModule, like so:<\/p>\n<p>@NgModule({<br \/>\n.<br \/>\n.<br \/>\nproviders: [<br \/>\n.<br \/>\n.<br \/>\nAlwaysAuthGuard<br \/>\n]\n})<\/p>\n<p>Finally we need to add this guard to one or more of our routes, lets add it to our ArtistComponent route like so:<\/p>\n<p>const routes: Routes = [<br \/>\n{path: &#8221;, redirectTo: &#8216;home&#8217;, pathMatch: &#8216;full&#8217;},<br \/>\n{path: &#8216;find&#8217;, redirectTo: &#8216;search&#8217;},<br \/>\n{path: &#8216;home&#8217;, component: HomeComponent},<br \/>\n{path: &#8216;search&#8217;, component: SearchComponent},<br \/>\n{<br \/>\npath: &#8216;artist\/:artistId&#8217;,<br \/>\ncomponent: ArtistComponent,<br \/>\ncanActivate: [AlwaysAuthGuard],<br \/>\nchildren: [<br \/>\n{path: &#8221;, redirectTo: &#8216;tracks&#8217;},<br \/>\n{path: &#8216;tracks&#8217;, component: ArtistTrackListComponent},<br \/>\n{path: &#8216;albums&#8217;, component: ArtistAlbumListComponent},<br \/>\n]\n},<br \/>\n{path: &#8216;**&#8217;, component: HomeComponent}<br \/>\n];<\/p>\n<p class=\"VSKILLbodytext\"><strong>IT Professionals, Web Developers, web programmers, IT students can Apply for the certification course and get ahead.<\/strong><\/p>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/web-development\/angular-4-certification\" rel=\"attachment wp-att-61497\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-61497\" src=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png\" sizes=\"auto, (max-width: 1050px) 100vw, 1050px\" srcset=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png 1050w, https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4-300x48.png 300w, https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4-768x124.png 768w, https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4-1024x165.png 1024w, https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4-712x115.png 712w\" alt=\"\" width=\"1050\" height=\"169\" \/><\/a><\/p>\n<ul>\n<li class=\"VSKILLbodytext\"><a href=\"https:\/\/www.vskills.in\/practice\/skills\/Web-Development\/Angular-4\" target=\"_blank\" rel=\"noopener\">Practice Test for Angular4 <\/a><\/li>\n<li class=\"VSKILLbodytext\"><a href=\"https:\/\/www.vskills.in\/certification\/web-development\/angular-4-certification\" target=\"_blank\" rel=\"noopener\">Govt Certification in Angular 4<\/a><\/li>\n<li><a href=\"https:\/\/www.vskills.in\/certification\/blog\/\">Blog posts on Angular 4 and Web Development<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/web-development\/angular-4\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-55483 size-full\" src=\"http:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/05\/Back-Arrow.jpg\" alt=\"Angular 4 Tutorial Index\" width=\"47\" height=\"52\" \/>Back to Angular 4 Tutorial Main Page <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular Router and Route Protection Angular Router is an official Angular routing library, written and maintained by the Angular Core Team. It\u2019s a JavaScript router implementation that\u2019s designed to work with Angular and is packaged as @angular\/router. First of all, Angular Router takes care of the duties of a JavaScript router: it activates all required&#8230;<\/p>\n","protected":false},"author":10,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[7230,7231,5743],"tags":[7221,7227,7235,7236,7226,7234,7389,7417,7402,7232,7223,7233],"class_list":["post-59900","page","type-page","status-publish","hentry","category-angular-2","category-angular-4","category-web-development","tag-angular","tag-angular-4","tag-angular-4-tutorial","tag-angular-cli","tag-angular-js","tag-angular-js-tutorial","tag-angular-route","tag-angular-route-protection","tag-angular-routes","tag-angular-tutorial","tag-angularjs","tag-angularjs-tutorial"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Angular Router and Route Protection - Tutorial<\/title>\n<meta name=\"description\" content=\"Angular Router and Angular Route Protection tutorial\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular Router and Route Protection - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Angular Router and Angular Route Protection tutorial\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:50:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/\",\"name\":\"Angular Router and Route Protection - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png\",\"datePublished\":\"2018-05-03T10:32:15+00:00\",\"dateModified\":\"2024-04-12T08:50:50+00:00\",\"description\":\"Angular Router and Angular Route Protection tutorial\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#primaryimage\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png\",\"width\":1050,\"height\":169},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Angular Router and Route Protection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Angular Router and Route Protection - Tutorial","description":"Angular Router and Angular Route Protection tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/","og_locale":"en_US","og_type":"article","og_title":"Angular Router and Route Protection - Tutorial","og_description":"Angular Router and Angular Route Protection tutorial","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:50:50+00:00","og_image":[{"url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/","name":"Angular Router and Route Protection - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#primaryimage"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#primaryimage"},"thumbnailUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png","datePublished":"2018-05-03T10:32:15+00:00","dateModified":"2024-04-12T08:50:50+00:00","description":"Angular Router and Angular Route Protection tutorial","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#primaryimage","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2018\/04\/Angular-4.png","width":1050,"height":169},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/angular-router-and-route-protection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Angular Router and Route Protection"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/59900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=59900"}],"version-history":[{"count":8,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/59900\/revisions"}],"predecessor-version":[{"id":64260,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/59900\/revisions\/64260"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=59900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=59900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=59900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}