9 - Route Guards and Products Lib

1. Add a lib for a products page

  • Add a lazy loaded lib with routing. We will grow this new feature lib out with advance NgRx features later int he course but for now we will just make the container component to navigate to on login.

nx generate @nrwl/angular:lib products --routing --lazy --parent-module=apps/customer-portal/src/app/app.module.ts
...
CREATE libs/products/README.md
CREATE libs/products/tsconfig.lib.json
CREATE libs/products/src/index.ts
CREATE libs/products/src/lib/products.module.ts
CREATE libs/products/tsconfig.json
CREATE libs/products/jest.config.js
CREATE libs/products/src/test-setup.ts
CREATE libs/products/tsconfig.spec.json
CREATE libs/products/.eslintrc.json
UPDATE package.json
UPDATE workspace.json
UPDATE nx.json
UPDATE tsconfig.base.json
UPDATE .vscode/extensions.json
UPDATE jest.config.js
UPDATE apps/customer-portal/src/app/app.module.ts
UPDATE apps/customer-portal/tsconfig.app.json
  • Add a products container component.

2. A default app route to always go to products page

  • Check default routes added to AppModule

  • Add a new default route to always load the products on app load

3. Add ProductsModule route

  • Login again at the '/auth/login' route to check the routing is correctly configured by trying to click the 'products' button in the main menu.

4. Add a route guard to protect products page

  • Generate a guard with the CLI

The CLI will ask you what functions to implement. We will only be needing CanActivate for now, so just press the ENTER key.

  • Add auth guard logic

5. Add auth guard to main routes

  • Re-export the guard from the index.ts file in the Auth module.

  • Apply the guard to the Customer Portal main routes.

Notice the syntax for lazy loading. It's shown in [the official docs])https://angular.io/guide/lazy-loading-ngmodules) as the --module option.

The docs also say it uses loadChildren followed by a function that uses the browser's built-in import('...') syntax for dynamic imports. The import path is the relative path to the module.

6. Check the route guard is working

  • Add a temporary "debugger" to set a break point on the route guard to check it is working correctly.

7. Cache the user in local storage to save logging in for the rest or the workshop

  • Load BehaviorSubject on Service creation with a user from local storage if one exists.

How to delete local storage

Extras

1. Add logout functionality

Try these steps below to logout a user.

  • Add logout button to main menu

  • Call service and logout the user by clearing the behaviour subject

  • Navigate to login page

2. Add angular interceptor

a) Update auth service to set a token in local storage

  • Add an angular interceptor in a new folder in the auth lib

Note: Currently there is no ng generate command for interceptors so we need to add it manually.

https://github.com/angular/angular-cli/issues/6937

  • Export auth interceptors from the auth module

b) Check the interceptor is adding a Header

  • Try and login again and look in the network traffic of the dev tools to see the Header is being added.

Authorization header on all requests

Last updated