User Account feature example

The following is a walkthrough of creating the Account feature.

Example change request

Overview

The User Account feature will be a new view that displays user account information to the authenticated user.

Acceptance criteria

  • Login as any user

  • Navigate to the user account view

    • New view should be available at the following URL: /account

  • Observe user information is displayed

    • Created at

    • Display name

    • Email

    • Phone number

    • Uid

    • Updated at

  • Update the application header 'Authentication' navigation menu to include an 'Account' link

    • Link should be displayed as the last menu item

  • Logout from user account

  • Navigate to /account

  • Observe unauthenticated user is redirected to /sign-in

Technical requirements

  • Account feature module

  • Account state management

  • Account service

    • Retrieve account information from the /users API

  • Account loaded guard

Generate Account feature library

Run the below command to generate the Account feature library.

The following project files should be created:

Project file tsconfig.base.json has also been updated to include the new project path and references should refer to this path.

Create Account component

Run the below command to generate the Account component

The following project files should be created:

Update Account route

Rename the routes file libs\features\account\src\lib\lib.routes.ts

  • From lib.routes.ts

  • To account-routing.module.ts

Update the account-routing.module.ts to be:

Update libs\features\account\src\lib\account.module.ts by removing all routing references and importing the new AccountRoutingModule

Update libs\features\account\src\index.ts and remove the routing export

Update application routing

Update the application routing file: apps\full-stack-typescript\src\app\app.routes.ts

Update the application navigation: apps\full-stack-typescript\src\app\app.component.html

The /account view should now be accessible.

Create Account service

Run the below command to create the Account service library:

The following files will be generated:

Delete the services-account.module.ts file.

Update the index.ts file to remove the module export.

Run the below command to create the Account Service

Update the index.ts file to export the account service file.

Implement NgRx state management

Run the below command to create the NgRx boilerplate.

The following files should be created.

Default NgRx schematics implements the entity pattern. This feature only loads a single user account and can use a simplified approach.

Delete the account.models.ts file.

Update actions

Update the libs\features\account\src\lib+state\account.actions.ts file to be:

Update reducer

Create the AccountState interface at libs\models\src\lib\account\account-state.interface.ts

Create the index.ts barrel file at libs\models\src\lib\account\index.ts

Update the barrel file to export the account-state.interface.ts file

Update the models barrel file at libs\models\src\index.ts to export the account folder

Update the libs\features\account\src\lib+state\account.reducer.ts file to be:

Update selectors

Create the ACCOUNT_FEATURE_KEY constant at libs\constants\src\lib\account\account-feature-key.constant.ts

Update the barrel file to export the account-feature-key.constant.ts file

Update the constants barrel file at libs\constants\src\index.ts to export the account folder

Update the libs\features\account\src\lib+state\account.selectors.ts file to be:

Update facade

Create the UserPartialState interface at libs\models\src\lib\account\account-partial-state.interface.ts

Update index.ts barrel file as required.

Update the libs\features\account\src\lib+state\account.facade.ts file to be:

Update effects

Update libs\features\account\src\lib+state\account.effects.ts to be:

Update Account module

Update libs\features\account\src\lib\account.module.ts for update import paths and dependencies.

Create account loaded resolver function

Generate an account library in the resolvers directory by running the below command:

nx g lib --directory=resolvers --name=account

Delete the resolvers-account.module.ts file.

Generate an account loaded resolver function in the resolvers directory by running the below command:

Update the account-loaded.resolver.ts file to be:

Updated the index.ts barrel file to export the resolver function.

Update the application routes to include the new guard.

apps\full-stack-typescript\src\app\app.routes.ts

Update Account component to retrieve account information

Update libs\features\account\src\lib\account.component.ts to be:

Update Account template to display account information

Update libs\features\account\src\lib\account.component.html to be:

Last updated