Skip to main content

Introduction

This tutorial demonstrates how to create an Angular application that allows users to log in using their Web3 wallets. After Web3 wallet authentication, the server creates a session cookie with a signed JWT stored inside. It contains session info (such as an address, signed message) in the user’s browser. Once the user is logged in, they will be able to visit a page that displays all their user data.

Prerequisites

  1. Follow the Your First Dapp - Angular tutorial to set up your Angular dapp and server

Install the Required Dependencies

To implement authentication using a Web3 wallet (e.g., MetaMask), we will use a Web3 library. For the tutorial, we will use @wagmi/core.
  1. Install @wagmi/core, @wagmi/connectors, [email protected], and axios dependencies.
npm2yarn
  1. Generate an environment file for our Angular app:
npm2yarn
  1. Open src/environments/environment.ts and src/environments/environment.prod.ts - add a variable of SERVER_URL for our server.
  1. We will generate two components (pages) - /signin (to authenticate) and /user (to show the user profile):
  1. Open src/app/app.routes.ts, add these two components as routes:

Initial Setup

We will do an initial setup of our /signin and /user pages to make sure they work before integrating with our server.
  1. Open src/app/signin/signin.component.html and replace the contents with:
  1. Open src/app/signin/signin.component.ts and add an empty handleAuth function below ngOnInit(): void {}:
  1. Run npm run start and open http://localhost:4200/signin in your browser. It should look like:
  2. Import NgIf in src/app/user/user.component.ts:
  1. Open src/app/user/user.component.html and replace the contents with:
  1. Open src/app/user/user.component.ts and add the variable we used above and an empty signOut() function:

Server Setup

Now we will update our server’s index.js for the code we need for authentication. In this demo, cookies will be used for the user data.
  1. Install the required dependencies for our server:
  1. Create a file called .env in your server’s root directory (where package.json is):
  • APP_DOMAIN: RFC 4501 DNS authority that is requesting the signing.
  • MORALIS_API_KEY: You can get it here.
  • ANGULAR_URL: Your app address. By default Angular uses http://localhost:4200.
  • AUTH_SECRET: Used for signing JWT tokens of users. You can put any value here or generate it on https://generate-secret.now.sh/32. Here’s an .env example:
  1. Open index.js. We will create a /request-message endpoint for making requests to Moralis.Auth to generate a unique message (Angular will use this endpoint on the /signin page):
  1. We will create a /verify endpoint for verifying the signed message from the user. After the user successfully verifies, they will be redirected to the /user page where their info will be displayed.
  1. We will create an /authenticate endpoint for checking the JWT cookie we previously set to allow the user access to the /user page:
  1. Lastly we will create a /logout endpoint for removing the cookie.
Your final index.js should look like this:
  1. Run npm run start to make sure your server runs without immediate errors.

Bringing It All Together

Now we will finish setting up our Angular pages to integrate with our server.
  1. Open src/app/signin/signin.component.ts. Add our required imports:
  1. Add this code to set up the Wagmi client:
  1. Replace our empty handleAuth() function with the following:
  1. The full signin.component.ts should look like:
  1. Open src/app/user/user.component.ts. Add our required imports:
  1. Replace ngOnInit(): void {} with:
  1. Replace our empty signOut() function with the following:
  1. The full user.component.ts should look like:
If you get errors related to default imports, open your tsconfig.app.json file and add "allowSyntheticDefaultImports": true under compilerOptions:

Testing the MetaMask Wallet Connector

Visit http://localhost:4200/signin to test the authentication.
  1. Click on the Authenticate via MetaMask button
  2. Connect the MetaMask wallet and sign the message
  3. After successful authentication, you will be redirected to the /user page
  • When a user authenticates, we show the user’s info on the page.
  • When a user is not authenticated, we redirect to the /signin page.
  • When a user is authenticated, we show the user’s info on the page, even refreshing after the page.