You can create and manage authentication in Laravel 8 easily using inbuilt packages. User authentication is always the most important concern of any web application. If you want to handle the application functionalities and roles then it always requires a user module. On the basis of the user, you can manage the rights of access in the application. I already shared a post on one of the latest features of Install Auth Laravel 8 for managing authentication using Jetstream and Livewire. In this post, I will show you how you can create authentication without using Jetstream. I will be going to use the Laravel UI package. Here, I will be starting with a new project in Laravel 8. So, let’s start.
Prerequisites
Before creating a new project in Laravel 8, your system must met the following requirements.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- VS Code Editor
- Composer
Create New Project in Laravel 8 For UI Authentication
For creating this project, I will be using the composer dependency manager. You can use the Laravel install too. So, let’s begin by creating a new project by hitting the below command in the terminal.
composer create-project --prefer-dist laravel/laravel authentication
The above command will start installing the Laravel 8 as showing here.
After creating the project, let’s open it in the editor and then we will install move to the next step.
Remove Uploaded Image in Laravel 8 From Public Folder
Create and Configure Database
For managing the database, I will be using MySQL 8 with the command line. Therefore, I opened the terminal and created a new database there.
CREATE DATABASE laravel8_auth;
After creating the database, let’s connect it with newly created Laravel application.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel8_auth
DB_USERNAME=root
DB_PASSWORD=root
Here, the database credentials are configured. Now, we can move to the next step for installing the Laravel UI Auth package for user authentication.
Install Laravel Authentication UI Auth Package
Laravel provides a default package for the user auth. So, here, we have to install the UI auth package. So, go inside the project directory, and hit the below command.
composer require laravel/ui
The above command will install the authentication package inside the project.
Once, the UI Auth package is installed, it will require to install the auth scaffolding. Now, Laravel wrapped the auth scaffolding inside the vue –auth package.
So, in the next step, we have to install the auth scaffolding.
How to Configure PHPMailer in Laravel 8 For Sending Email
Install Auth Scaffolding in Laravel 8
For managing the authentication, Laravel requires the auth scaffolding. It will provide the complete authentication of user like Login, Register, Forgot Password, etc.
php artisan ui vue --auth
The above command will add the vue auth scaffolding in the project.
After adding the auth scaffolding, it will require to install the node dependencies. It will add the CSS and js file for the UI.
Install npm in Laravel 8 For UI Auth
To install the npm, you will have to run the below command.
npm install
The above command will install the npm. But after installing the npm, it will require hitting the npm run dev command. It will compile the installed npm package. You can run the both command together or either you can run one by one.
npm run dev
You can run both commands together as showing below –
npm install && npm run dev
After compiling the npm, it will add two folders inside the public directory of the project.
- css – In this folder, there is a CSS file named app.css.
- js – It contains a js file named app.js.
These files are linked with the master view file of user authentication.
If you will run the project, you will see the result as showing below.
In the next step, you will have to migrate the database. So, that the necessary tables will be generated inside the database.
Dynamic Email Configuration in Laravel 8 For Sending Email
Migrate tables for Laravel Authentication
For migrating the tables, we have to run the below command.
php artisan migrate
Here, in the migration, it will add the following tables as showing below.
After the migration tables are created in the database. Now, we can use the authentication functionalities created by the UI Auth package.
UI Auth Package Functionality
Firstly, when you click on the Register option in the dashboard, then you have the register page as showing below.
Here, all the validations are set by default. Like email address, password and password confirmation. So, once, you will register, you will be redirected to the dashboard.
In the dashboard, you can see the profile name of the logged user. So, there is an option to logout. When you will logout, you will be redirected to the login page. Here is the login page.
In the login page, there is a password reset option. So, when you will click on that you will have the below page.
In the above form, you can enter the email address on that you will get an email of password reset link. But, it will not work directly because we haven’t configured the SMTP detail for the email configuration.
So, if you try then you will get the error as showing below. Here, the host and other details are not configured.
Now, let’s fix this issue so that we will be able to get the password reset link on email.
Laravel 8 Client Side Form Validation Using jQuery
Configure Email For Sending Password Reset Link
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME={{YOUR_MAIL_USERNAME}}
MAIL_PASSWORD={{YOUR_MAIL_PASSWORD}}
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS={{FROM_ADDRESS}}
MAIL_FROM_NAME="${APP_NAME}"
After configuring the above credentials, you can send the password reset link email.
Here, I have sent the password reset link on my email address. You can see the response.
The password reset email has been sent, and here I got the email in my mailbox.
You can reset your password by clicking on the Reset Password button or through the link.
Create RESTful APIs in Laravel 8 Using Sanctum Auth
Reset Password in Laravel Authentication Using UI Auth
After clicking on the link, it will redirect you to the password reset page. Here, the email address is already filled. So, you have to enter the new password and then confirm.
After successfully resetting the password, you will get a successful response. Here, the password has changed successfully.
So, using Laravel authentication, you can manage all these functionalities.
Conclusion
Laravel UI auth package provides the authentication system for the user. It allows us to register, login, password reset with email verification functionalities. It is very easy to install and manage. You can configure the email settings before sending email for the password reset. So, I hope you can implement the Laravel authentication system without using Jetstream. If you want an easy step to manage authentication then you can opt UI Auth package.
Leave a Reply