From the course: Angular: API Communication and Authentication

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Preventing route access without authentication

Preventing route access without authentication - Angular Tutorial

From the course: Angular: API Communication and Authentication

Start my 1-month free trial

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…

Contents