DotNet-301-ElevenNoteAngular
  • Introduction
  • Part 1: Setup with the Angular CLI
  • Part 2: Building out the Header Component
  • Part 3: Sprucing up the Header Using Material
  • Part 4: Building the Registration Page with Reactive Angular Form
  • Part 5: Routing with Angular Router
  • Part 6: Design Break: Fonts
  • Part 7: Communicating with Web API with Auth Service
  • Part 8: Logging in with a token
  • Part 9: Routing After a Request
  • Part 10: Log In page Challenge
  • Part 11: Building the Note Index page
  • Part 12: Creating Note C.r.u.d. — Create
  • Part 13: Creating Note c.R.u.d. — Read
  • Part 14: Creating Note c.r.U.d. — Update
  • Part 15: Creating Note c.r.u.D. — Delete
  • Part 16: Protecting our views with Guards
Powered by GitBook
On this page
  • Step 1. Building out the our Auth Guard
  • Step 2. Implementing the Auth Guard on our paths
  • Step 3. Running through Slight design changes here and there

Part 16: Protecting our views with Guards

PreviousPart 15: Creating Note c.r.u.D. — Delete

Last updated 7 years ago

Protecting our views with Guards

So right now all of our views are accessible by anyone. If a non logged in user tries to access the route /notes, they will have access but they won’t see anything because they don’t have an access token.

What Guards will allow us to do is protect certain routes based on different criteria. So that we create rule of how users move throughout our application, whether logged in, or not.

Read more about Guards .

Step 1. Building out the our Auth Guard

We will start by creating a new folder called guards in our app folder. Then, following Angular naming styles, we are going to create a file called auth.guard.ts.

To restrict access based on who the user is we use the CanActivate class. With using this class we need to create a method called canActivate(), that will run when this guard is called automatically.

The canActivate() method needs to return a boolean observable, so let’s build that out.

The method, checks to see if there is a access_token currently in the localStorage then a user must be logged in. If there isn’t then we will navigate the user to the login screen. If there is a access_token then they will be able to see the views that we restricted.

Step 2. Implementing the Auth Guard on our paths

For us to use the auth guard we need to implement the canActivate guard on the path we are trying to restrict.

Let’s go to our app.module.ts file. And add it to our notes path.

Then we need to add the Auth Guard to our providers property located in the @NgModules Decorator.

Step 3. Running through Slight design changes here and there

For a style change, I am fond of adding some padding for our main content. We are going to add a div with a container around out router-outlet directive in the app.component.html file. And then, add the container class in the app.component.css

here
Logo Title Text 1
Logo Title Text 1
Logo Title Text 1
Logo Title Text 1