cover

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.

Database migrations

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.

Install PyMySQL

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.

craft migrate

Migrate

Authentication

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.

craft auth

After running the command, your routes/web.py file will look like that:

routes/web file

To see the routes added, run:

craft show:routes

Route list

Now, you can start your development server, with:

craft serve

Visit http://localhost:8000/register

Register page Login page Dashboard

Reset Password

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

In this tutorial, we used mailtrap.io. Please go to mailtrap.io and create an account. After logging in, click on demo and select Laravel under the Integrations sections:

Mailtrap dashboard

Then, open the .env and put this in that file:

MAIL_DRIVER=smtp
MAIL_FROM_ADDRESS=from@example.com
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.

Reset password

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.

Forgot password page

Verify your mailtrap dashboard, and you will see the email sent.

Email page

Visit the link you will received and reset your password. Reset password page

See the full code in this repo: https://github.com/sphinxxanxus/user_auth

Conclusion

Make the authentication with masonite is very easy because masonite comes with a default implementation of the authentication which is fully customizable.