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
1.Authentication
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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Step 2. Create a function to load the view of your registration page.
public function index()
{
return view('create_acc');
}

Step 3. Now create a function to store the data of the user while registering
public function create(Request $r)
{
$name=$r->uname;
$email=$r->uemail;
$pass=$r->u_pass;
$check_email=App\Login::where('email',$email)->get();
if(count($check_email)>0) ////this is to check duplicate email insertion
{
return redirect('/create_account')->with('msg','Email Exists');
}else{
$login= new App\Login;
$login->name= $name;
$login->email= $email;
$login->pass= $pass;
$created=$login->save();
if($created){
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 form.to do so create a function to check the user is authentic or not
public function check_user(Request $r)
{
$email=$r->uemail;
$password=$r->u_pass;
$session= App\Login::where('email',$email)->where('pass',$password)->get();
//if email and password match with the database then
if(count($session)>0){
//now we have to store the data to the session
$r->session()->put('user_id',$session[0]->id);
$r->session()->put('user_name',$session[0]->name);
return redirect('/welcome');
}else{
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
$r->session()->put('user_id',$session[0]->id).
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)
{
if($r->session()->get('user_id')=="")
{
return redirect('/login');
}else{
$username=$r->session()>get('user_name');
$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)
{
$r->session()->forget('user_id');
$r->session()->forget('user_name');
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.