Laravel 7 custom login and logout with the session.

  • Post author:
  • Post category:Laravel
  • Post comments:0 Comments
  • Post last modified:January 22, 2021
  • Reading time:4 mins read
In this blog, you are going to learn how to set Laravel 7 custom login and logout with the session. (i.e, Laravel authentication).

In LARAVEL if you want to perform login and log out to secure a certain page or multiple pages of your page to check whether the user is authentic or not there are two way
2.Custom Laravel session login and logout.

What is Laravel Authentication?

Authentication nothing but a pre-built authentication controller and it is very easy to set up.

Just go to your command prompt and type the following

php artisan make: auth.
After executing the command go to your project folder, you will find auth.php located at config/auth.php.How to set up auth and how to implement related functions I will make another video on it and will be making another blog for it.
Now, basically, we are going to learn how to set custom session and use it as per our need. It is not so hard it is very easy, simply go to your command prompt and create a controller(example: Login_con) and a model(Example: Login_mod) respectively as per your need (if you don’t know how to create a controller and model check the following link Laravel routes and basic folder structure understanding ).

Step 1. Create a table to store the user credentials so that it stores the data while the user registers.

Example Database structure given below you can create a table as per your need:

`id` int(11) NOT NULL, 

CREATE TABLE `users` (

`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,

`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`updated_at` datetime NOT NULL DEFAULT current_timestamp()
Laravel session login and logout
Laravel login table

Step 2. Create a function to load the view of your registration page.

public function index()
	return view('create_acc');
Laravel session login and logout
Registration form in Laravel

Step 3. Now create a function to store the data of the user while registering

public function create(Request $r)

	if(count($check_email)>0) ////this is to check duplicate email insertion
		return redirect('/create_account')->with('msg','Email Exists');

	$login= new App\Login;

		$login->name= $name;
		$login->email= $email;   	
		$login->pass= $pass;  



			return redirect('/login')->with('msg','Account Created Successfully. Please LogIn !');



Step 4. Create a function to load the view of your Login page.

public function login()
	return view('login');

Step 5. Now, we have to operate the data which has been provided by the user in the login do so create a function to check the user is authentic or not

public function check_user(Request $r)

			$session=  App\Login::where('email',$email)->where('pass',$password)->get();

			//if email and password match with the database then 


				//now we have to store the data to the session


				return redirect('/welcome');


					return redirect('/login')->with('msg','Email or Password does not match');


As you can on the above mantioned code ,to set data in the session we have to use


Step 6. Now create a function to protect or secure your desired page or function

so that any unauthenticated user can not get access to that particular page.
To do so, create a function

public function protect(Request $r)

   return redirect('/login');


 $capsule = array('username' => $username);

 return view('protect')->with($capsule);

Step 7. Now it’s time to log out, Just create a function for log out so that when the user logs out the session gets unset and destroy.

public function logout(Request $r)

			return redirect('/login');


Check out the web.php

Route::get('/create_account','[email protected]');

Route::post('/create','[email protected]');

Route::get('/login','[email protected]');

Route::post('/check','[email protected]_user');

Route::get('/welcome','[email protected]');

Route::get('/logout','[email protected]');

For better understanding check out the video.

Live demo Click Here.

Leave a Reply