From the course: Angular: API Communication and Authentication
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Preventing route access without authentication - Angular Tutorial
From the course: Angular: API Communication and Authentication
Preventing route access without authentication
- [Narrator] Before we wrap up this project, there is one more thing that we need to tackle. Let's first start our application here using npm start. Once things are ready, head over to your browser and let's request the root page. A local host port 3000. You'll notice that I am redirected to the contacts route by default. Of course, I can't see the data, because I haven't logged in. So, there is no token saved locally, but we should still prevent such behavior and then set redirect users to the login page if they are not logged in. We can protect routes using what is called a route guard. A route guard is simply used to perform certain checks when the route is being requested and it can decide whether or not to continue with the navigation. Let's kill the server here and let's generate a new guard using the CLI. Aside from generating components and services, the CLI can also generate a guard for you. Let's call this guard auth. First thing we need to do is add it to the providers…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.