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
- 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.- Install
@wagmi/core,@wagmi/connectors,viem@2.x, andaxiosdependencies.
npm2yarn
- Generate an environment file for our Angular app:
npm2yarn
- Open
src/environments/environment.tsandsrc/environments/environment.prod.ts- add a variable ofSERVER_URLfor our server.
- We will generate two components (pages) -
/signin(to authenticate) and/user(to show the user profile):
- 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.
- Open
src/app/signin/signin.component.htmland replace the contents with:
- Open
src/app/signin/signin.component.tsand add an emptyhandleAuthfunction belowngOnInit(): void {}:
- Run
npm run startand openhttp://localhost:4200/signinin your browser. It should look like: - Import
NgIfinsrc/app/user/user.component.ts:
- Open
src/app/user/user.component.htmland replace the contents with:
- Open
src/app/user/user.component.tsand add the variable we used above and an emptysignOut()function:
Server Setup
Now we will update our server’sindex.js for the code we need for authentication. In this demo, cookies will be used for the user data.
- Install the required dependencies for our server:
- Create a file called
.envin your server’s root directory (wherepackage.jsonis):
- 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.envexample:
- Open
index.js. We will create a/request-messageendpoint for making requests toMoralis.Authto generate a unique message (Angular will use this endpoint on the/signinpage):
- We will create a
/verifyendpoint for verifying the signed message from the user. After the user successfully verifies, they will be redirected to the/userpage where their info will be displayed.
- We will create an
/authenticateendpoint for checking the JWT cookie we previously set to allow the user access to the/userpage:
- Lastly we will create a
/logoutendpoint for removing the cookie.
index.js should look like this:
- Run
npm run startto 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.- Open
src/app/signin/signin.component.ts. Add our required imports:
- Add this code to set up the Wagmi client:
- Replace our empty
handleAuth()function with the following:
- The full
signin.component.tsshould look like:
- Open
src/app/user/user.component.ts. Add our required imports:
- Replace
ngOnInit(): void {}with:
- Replace our empty
signOut()function with the following:
- The full
user.component.tsshould look like:
tsconfig.app.json file and add "allowSyntheticDefaultImports": true under compilerOptions:
Testing the MetaMask Wallet Connector
Visithttp://localhost:4200/signin to test the authentication.
- Click on the
Authenticate via MetaMaskbutton - Connect the MetaMask wallet and sign the message
- After successful authentication, you will be redirected to the
/userpage
- When a user authenticates, we show the user’s info on the page.
- When a user is not authenticated, we redirect to the
/signinpage. - When a user is authenticated, we show the user’s info on the page, even refreshing after the page.

