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
- Create a Moralis account.
- Install Python 3 (in case you don’t already have it). In this tutorial, we used Python 3.10 on a Windows system.
- Basic Django knowledge (Django documentation).
Installing Required Dependencies
- Create a virtual environment if needed:
python3 -m venv django_web3_auth_env. - Install
djangoandrequestsdependencies. 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
- Create the Django project:
django_web3_auth_env\Scripts\django-admin startproject moralis_authanddjango-adminwill be found in theScriptsfolder:django_web3_auth_env\Scripts\django-admin.exe.
- 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_authinto the same folder where themoralis_authproject is in - the same folder wheremanage.pyis located.
- 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.
- 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
- Add the newly created app named
web3_authto the list of installed apps insettings.pyat the end of theINSTALLED_APPSlist:
settings.py
- Include URLs from the newly created app in the new project (here, we also added the URLs from
django.contrib.auth.urlsto be able to use the log-out functionality):
urls.py
Creating the Main web3_auth Application (urls.py, views.py, and Templates)
- The contents for
urls.py(you will have to create this file):
urls.py
moralis_authwill contain the data from where a user can authenticate.request_messagewill make a request to the Moralis Auth API for a message to be signed.my_profilewill show current profile info for a user when authenticated.verify_messagewill be used to verify a message that was signed.
- 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
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.
- 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).
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:
