Skip to main content

Introduction

In this tutorial, we show you how to create a full-stack Django app that allows users to log in using their Web3 wallets, and Django will create a session associated with the individual user. Once logged in, the user can visit a page that displays all their user data. You can find the repository with the final code here.

Prerequisites

  1. Create a Moralis account.
  2. Install Python 3 (in case you don’t already have it). In this tutorial, we used Python 3.10 on a Windows system.
  3. Basic Django knowledge (Django documentation).

Installing Required Dependencies

  1. Create a virtual environment if needed: python3 -m venv django_web3_auth_env.
  2. Install django and requests dependencies. Django version 4.1 was used for this tutorial:
    • django_web3_auth_env\Scripts>pip3.10.exe install django.
    • django_web3_auth_env\Scripts>pip3.10.exe install requests.
      (These commands, for example, pip3.10.exe install django, are meant to be executed in that specific Scripts folder from that virtual environment.)

Creating a Django Project and App

  1. Create the Django project:
    • django_web3_auth_env\Scripts\django-admin startproject moralis_auth and django-admin will be found in the Scripts folder: django_web3_auth_env\Scripts\django-admin.exe.
  2. Create the Django app:
    • django_web3_auth_env\Scripts\python.exe manage.py startapp web3_auth.
    • You can move that newly created app folder named web3_auth into the same folder where the moralis_auth project is in - the same folder where manage.py is located.
  3. Run database migrations:
    • django_web3_auth_env\Scripts\python.exe manage.py migrate. Here, you will have to use the complete path that points to the Python executable in the newly created virtual environment.
  4. Create a super user (it can be used in the Django admin interface); it is optional:
    • django_web3_auth_env\Scripts\python.exe manage.py createsuperuser. Here, you will have to use the complete path that points to the Python executable in the new created virtual environment.

Edit moralis_auth Project Settings

  1. Add the newly created app named web3_auth to the list of installed apps in settings.py at the end of the INSTALLED_APPS list:
settings.py
  1. Include URLs from the newly created app in the new project (here, we also added the URLs from django.contrib.auth.urls to be able to use the log-out functionality):
urls.py

Creating the Main web3_auth Application (urls.py, views.py, and Templates)

  1. The contents for urls.py (you will have to create this file):
urls.py
  • moralis_auth will contain the data from where a user can authenticate.
  • request_message will make a request to the Moralis Auth API for a message to be signed.
  • my_profile will show current profile info for a user when authenticated.
  • verify_message will be used to verify a message that was signed.
  1. The contents for views.py (you will need to set your Web3 API key on line nine [API_KEY = 'WEB3_API_KEY_HERE']):
views.py
Here we have a view for the main authentication: moralis_auth; one view to display the profile info: my_profile; and two views specific to authentication: request_message and verify_message. Furthermore, verify_message will request a message from the Moralis Auth API that will be signed with MetaMask, and verify_message will validate the received signature and create a user when the validation succeeds. After that, a session is created for that user, and we can add additional info in that session, such as the data that was used specifically for authentication.
  1. Templates (you will have to create a folder named templates):
  • login.html, this template contains all the JavaScript code required to sign a message with MetaMask:
login.html
  • profile.html, this template only shows current info associated with an authenticated user:
profile.html

Starting the Application

  • django_web3_auth_env\Scripts\python.exe manage.py runserver 1000 (this will start a local server on port 1000).
After the application starts, this is how it should look when you access http://127.0.0.1:1000/web3_auth/moralis_auth This will show when clicking on the above “login” button After the message is signed and the authentication is successful, you can see the complete profile page: