This is a simple tutorial on implementing authentication in your Masonite application. Masonite comes with the basic implementation of the authentication system. In this tutorial, I would like to share with you how to make user authentication using Masonite 2.3.
The first step to set up our authentication system is to configure your database and migrate the default migrations provided by Masonite.
Configure your database credentials
Open your .env file and modifies these variables like:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=user_auth DB_USERNAME=root DB_PASSWORD=password
Please make sure you provide the right credentials for your database.
In this tutorial, we used MySQL. Before using Masonite with a MySQL database, you need to install one of these packages: PyMySQL or mysqlclient. We will use PyMySQL. To install it, use the command below:
pip install PyMySQL
Migrate the migrations
After you provided the credentials of your database, run this command to perform the migrations.
In this section, we will see how you can use the default user authentication implemented by Masonite. The command below creates all the files you need. It will create some files and replace others.
After running the command, your routes/web.py file will look like that:
To see the routes added, run:
Now, you can start your development server, with:
Masonite provides a way to reset a password. To use it, we need to configure the mail variables existing in .env.
Configure the mail variables
Then, open the .env and put this in that file:
MAIL_DRIVER=smtp MAIL_FROM_ADDRESSfirstname.lastname@example.org MAIL_FROM_NAME=Example MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=Username MAIL_PASSWORD=Password
The password and username are provided by mailtrap please copy and paste in the .env file.
After the previous configuration, open http://localhost:8000/password in your browser before restarting your development server.
Enter your email and you will receive the link you should use to reset the password of the user with the email entered before.
Verify your mailtrap dashboard, and you will see the email sent.
Visit the link you will received and reset your password.
See the full code in this repo: https://github.com/sphinxxanxus/user_auth
Make the authentication with masonite is very easy because masonite comes with a default implementation of the authentication which is fully customizable.