User authentication and data security access are the most important key for any database system. If you are creating an application and the login access of that application is not much secure and authentic. Then someone can manipulate or access your data. So, to prevent these types of un-authentic access, we will need to create proper authentication. Today, in this Laravel 6 tutorial series, I will be dealing with the Laravel 6 Login and Registration with the proper authentication. In Laravel, there is an inbuilt functionality for authentication. So, we don’t need to put extra effort and code for creating auth.
Prerequisites
For creating this project, I have the latest version of PHP (7.3.9), MySQL (8.0.17). You can check the MySQL version in Windows using the below command.
mysqld --version
For editor, I am using Visual Studio code. You can use PHP Storm, Atom, etc.
Laravel 6 Login and Registration
Before creating this project, I am assuming that your system is ready for creating the Laravel 6 project. If not then Install Laravel 6 in Windows and Ubuntu. Open the command prompt or terminal and create a new Laravel 6 project.
composer create-project --prefer-dist laravel/laravel user-registration-app
It will take some time to create the project. After creating the project just run it by the php artisan command.
php artisan serve
As a result, you will see the default homepage of the Laravel is running.
- In the next step, I will install the auth in Laravel 6.
- But, before adding auth, we need to add frontend scaffolding in the project.
- In Laravel 6, this frontend scaffolding has been moved to the ui –dev package.
- Firstly, we will require to add this package.
composer require laravel/ui --dev
Secondly, we’ll add the auth using the ui vue –auth command.
php artisan ui vue --auth
How to Integrate Laravel 6 Application with Firebase Realtime Database
After adding the authentication scaffolding, run your project again. Now, you can see, the Laravel auth has been added Login and Register functionalities on the Homepage.
Next, I will install the front end dependencies in the application so that we will not be required to add the Bootstrap CSS and js file. Laravel will add these necessary files automatically inside the project.
Laravel 6 RESTful APIs with Passport Authentication
Install Front end Dependencies
Laravel 6 comes with the default CSS, JS, and pre-processor. Therefore, we can create our application by using these dependencies.
So, install npm inside the project directory.
npm install
After adding these packages, we’ll need to run the development of these packages. So, It can add the Asset files in the public directory of the project.
Navigate to the public folder of the project, You will see the Asset files like CSS and js files.
Create a Database and Configure
Open the MySQL database and create a database. In my case, my database name is registration.
create database registration;
You no need to create any table here. Now, open the .env file and change the database credentials with below snippet. Replace database username and password with yours.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=registration
DB_USERNAME=root
DB_PASSWORD=root
The next step will move for migrating the migration file for the table which has been created by default.
How to Upload Files and Images in Laravel 6 with Validation
Create User Migration
Laravel 6 provides a default Model for the User. So, you don’t need to create a User model. Rather than, just need to add the fillable data that you want to add to the database table. This will be the default code of the User.php file.
// User.php
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
So, open the database folder and then navigate to the migrations. Under the migration folder, by default, you will find three migration files that have been created with the project creation. The migration file mainly defines the schema of the table which is going to be created in the database.
Here, we can add or manipulate these schemas and Laravel will maintain all the version of the migration files which are altered.
Basically, in this post, I am working with Laravel 6 login and registration, therefore, I am not going to create more migration files. So, just open the create_users_table.php file. Your table schema will look the same as below.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Now, run the artisan command to migrate the schema into the database.
php artisan migrate
After the successful migration, show the tables inside the MySQL database.
Laravel 6 CRUD Application with Form Validation
Laravel 6 User Registration
On the homepage, just click on the Register link first. You will get a registration form that will like the same as below.
This registration form comes up with the default form validation and it will work fine. So, it will validate the email and other fields properly.
Here, I have entered a password to check whether it is going to be registered or not. So, as a result, you can see here, It didn’t allow me to register before validating the form fields.
Laravel 6 RESTful APIs for ToDo Application with Passport Auth
User Dashboard in Laravel 6
After successful registration, you will be redirected to the default dashboard page. Here, you will see a message You are logged in!. It means you are inside the dashboard page.
In the right corner, you will see your registered name. When you click on the name, you will see the Logout option. When you will logout the profile, it will redirect you to the default homepage.
Laravel 6 User Login
When you will redirect to the homepage, again you will have the option for login and registration. Now, click on the Login link. Now, you will have the user login page which has created by auth scaffolding.
The form validation will work for the login form too. Here, I had tried with an invalid password and it has thrown an error.
After providing the correct login details, it will be redirected to the Dashboard page. Now, you are logged in.
So, here we have logged in successfully in the dashboard. This is a basic user registration and login which is provided by the Laravel by default.
Conclusion
Bingo! we have created a basic user registration and login with auth in the Laravel 6. You can design the form as per your project requirements. I hope, this post will help you in creating your projects.
abdullah says
is this professional way to create login system using auth and customized the design of login,registration, etc pages