Update User Account callable function example

The following is a walkthrough of creating the Update User Account function example

Example change request

Overview

Users need to be able to update their accounts with a Github URL.

Acceptance criteria

  • Login as any user

  • Navigate to the user account view

    • /account

  • Observe view includes the following:

    • Update Github URL: <input>

    • Button to update the user's Github URL

Technical requirements

  • Account template updates to include new design

  • Account component update to interact with the state

  • Account state management updated to interact with API

  • onUpdateAccount HTTPS callable function to update the /users/{{uid}} document with new Github URL property

Type of function

For this update, we will be creating an HTTPS callable functionarrow-up-right that allows users to update their account information directly from the application.

Additional types of functions can be reviewed here: Cloud Functionsarrow-up-right

Create onUpdateAccount function

In the apps\full-stack-typescript-api\src\functions folder, create the following:

Update apps\full-stack-typescript-api\src\functions\account\index.ts to be:

The new function receives a data payload of type User and merges the new data to the users document in our database.

Update apps\full-stack-typescript-api\src\main.ts to include the new function.

The main.ts file is our primary entry point for our functions and needs to be updated so that our new function is included when the project is built.

Build the functions

Run the following command to build the functions application:

nx build full-stack-typescript-api

Deploy functions

Run the following command to deploy new and updated functions:

firebase deploy --only functions

Update the frontend application for new API interaction

Now that we've created a secure method for interacting with the database, we need to update the frontend application to integrate with the new function.

Update Account state management

Update actions

Update libs\features\account\src\lib\+state\account.actions.ts to include the new actions:

  • updateAccount

  • updateAccountFailure

  • updateAccountSuccess

Update reducer

Update libs\features\account\src\lib\+state\account.reducer.ts to include:

Update facade

Update libs\features\account\src\lib\+state\account.facade.ts to include:

Update effects

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

Update AccountService

Update libs\services\account\src\lib\account.service.ts to include:

Update AccountModule for forms integration

Update libs\features\account\src\lib\account.module.ts to include the FormsModule :

Update AccountComponent class

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

Update AccountComponent template

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

Further improvements

There is now a secure way for users to update their account information with the database. The onUpdateAccount function and Account state accepts a User payload which can include all of the attributes of User.

The AccountComponent can be improved to enable updating of all User properties.

Last updated