Skip to content
8bityard
8bityard
  • Laravel
  • WordPress
  • PHP
  • Html
  • Bootstrap
  • J query
  • Tools
    • Word Counter
8bityard
8bityard
  • About
  • Contact
  • Portfolio
  • Post
  • Privacy Policy
  • Terms and Conditions
  • Welcome to 8bityard

How to Implement Remember me Functionality in Laravel 8?

/ Laravel / By Gaurav Pandey

In this tutorial, I will give you an example of “How to Implement Remember me Functionality in Laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

remember me in laravel
cookie in laravel

Remember Me Functionality

The Remember me feature allows the client-side users to automatically remember their user login details as they regularly visit the web application. In most cases, the user login information is stored in the form of a cookie in his web browser.

Cookies in Laravel

Cookies are small data files, which are stored in the remote browser. And by the help of cookies tracking/identifying return users in web applications. You can get/fetch, set/create and delete/destroy cookies in laravel using the cookies methods

Let’s Start with Scratch :

We implement the remember me functionality in login form in laravel application.

Firstly, we create a register and login form, and we retrieve the parameters of login forms on the form submit and also we check the check box is checked or not, and we write the code for the cookie.

app\Models\User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, 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',
    ];
}

routes\web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestRemembermeController;

#Remember me functionality in Laravel
Route::get('/user-register',[TestRemembermeController::class,'registerform'])->name('user.register');
Route::post('/post-registration',[TestRemembermeController::class,'postRegistration'])->name('post.register');

Route::get('/user-login',[TestRemembermeController::class,'loginform'])->name('user.login');
Route::post('/check-login',[TestRemembermeController::class,'checklogin'])->name('post.login');

Route::get('/user-dashboard',[TestRemembermeController::class,'dashboard'])->name('user.dashboard');
Route::get('logout', [TestRemembermeController::class, 'logout'])->name('logout');

Create a Controller

php artisan make:controller TestRemembermeController

app\HTTP\Controllers\TestRemembermeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use\App\Models\User;
use Auth;
use Session;
use Illuminate\Support\Facades\Hash;

class TestRemembermeController extends Controller
{

    public function registerform()
    {
        return view('rememberme.signup');
    }

    public function postRegistration(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);
           
           $usersignup = new User;
           $usersignup->name      = $request->name;
           $usersignup->email     = $request->email;
           $usersignup->password  = Hash::make($request->password);
           $usersignup->save();
           return redirect()->route('user.login');

    }

    public function loginform()
    {
        return view('rememberme.login');
    }

    public function checklogin(Request $request)
    {
          $input = $request->all();
          $this->validate($request, [
              'email' => 'required',
              'password' => 'required',
           ],[
              'email.required' => 'Email is required',
              'password.required' => 'Password is required',
             
            ]);

             if($request->rememberme===null){
                setcookie('login_email',$request->email,100);
                setcookie('login_pass',$request->password,100);
             }
             else{
                setcookie('login_email',$request->email,time()+60*60*24*100);
                setcookie('login_pass',$request->password,time()+60*60*24*100);

             }
             if (Auth::attempt(['email' => $input['email'], 'password' => $input['password']]))
             {
                Session::put('user_session', $input['email']);
                return redirect('/user-dashboard');
             }
             else
             {
                dd('Invalid credentials!');
            }
        }

    public function dashboard()
    {
        return view('rememberme.dashboard');
    }

    public function logout()
    {
        Auth::logout();
        Session::forget('user_session');
        return redirect()->route('user.login');
    }
}

resources\views\rememberme\signup.blade.php

remember me functionality in laravel
<div class="container">
         <div class="row justify-content-center">
            <div class="col-lg-6">
               <div class="main">
                  <h3><a>Remember me in Laravel Example</a></h3>
                  <form role="form" action="{{route('post.register')}}" method="post">
                     @csrf
                     <div class="form-group">
                        <label for="userename">Name <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control">
                         @if ($errors->has('name'))
                        <span class="text-danger">{{ $errors->first('name') }}</span>
                        @endif
                     </div>
                     <div class="form-group">
                        <label for="useremail">Email <span class="text-danger">*</span></label>
                        <input type="email" name="email"  class="form-control">
                         @if ($errors->has('email'))
                        <span class="text-danger">{{ $errors->first('email') }}</span>
                        @endif
                     </div>
                     <div class="form-group">
                        <label for="userpassword">Password <span class="text-danger">*</span></label>
                        <input type="password" name="password" class="form-control">
                         @if ($errors->has('password'))
                        <span class="text-danger">{{ $errors->first('password') }}</span>
                        @endif
                     </div>
                     <div class="form-group">
                     </div>
                     <button type="submit" class="btn btn btn-secondary">
                     Register
                     </button>
                  </form>
               </div>
            </div>
         </div>
      </div>

resources\views\rememberme\login.blade.php

In the login blade file we check the email and password is exist on cookie or not in our browser and then we write the conditions, like Cookie:make(), Cookies::get(), Cookies::forget().

login form with remember me functionality in laravel
<div class="container">
         <div class="row justify-content-center">
            <div class="col-lg-6">
               <div class="main">
                  <h3><a>Remember me in Laravel Example</a></h3>

                   @php if(isset($_COOKIE['login_email']) && isset($_COOKIE['login_pass']))
                   {
                      $login_email = $_COOKIE['login_email'];
                      $login_pass  = $_COOKIE['login_pass'];
                      $is_remember = "checked='checked'";
                   }
                   else{
                      $login_email ='';
                      $login_pass = '';
                      $is_remember = "";
                    }
                   @endphp
                  <form role="form" action="{{route('post.login')}}" method="post">
                     @csrf
                     <div class="form-group">
                        <label for="useremail">Email <span class="text-danger">*</span></label>
                        <input type="email" name="email" value="{{$login_email}}"  class="form-control">
                         @if ($errors->has('email'))
                        <span class="text-danger">{{ $errors->first('email') }}</span>
                        @endif
                     </div>
                     <div class="form-group">
                        <label for="userpassword">Password <span class="text-danger">*</span></label>
                        <input type="password" name="password" value="{{$login_pass}}" class="form-control">
                         @if ($errors->has('password'))
                        <span class="text-danger">{{ $errors->first('password') }}</span>
                        @endif
                     </div>
                      <div class="checkbox pull-right">
                        <label>
                        <input type="checkbox" name="rememberme" {{$is_remember}}>
                        Remember me </label>
                     </div>
                     <button type="submit" class="btn btn btn-secondary">
                     Login
                     </button>
                  </form>
               </div>
            </div>
         </div>
      </div>

When we enter email and password in our login form, we check the remember me checkbox, and then we log out, when we log in again to our application email and password are already filled if we checked to remember me checkbox.

resources\views\rememberme\dashboard.blade.php

<h1>Welcome , {{ Auth::user()->name }}</h1> 
<a href="{{route('logout')}}">Logout</a>

You can set the cookie for an interval like minutes, hours, day, weak, month, and year.

cookie storage in browser in laravel

To view remember me cookie in your Laravel application click on i-icon besides URL field and then select cookie dropdown in Google Chrome and Mozilla Firefox you can see options find cookies used by your laravel site under option cookies.

checkbox checked using cookie remember me in laravel

Finally, In this tutorial, we learned, Implement Remember me Functionality in Laravel 8, I hope this article will help you with your Laravel application Project.

Read Also : Append a trailing slash in every route in Laravel.

Gaurav Pandey

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Post navigation
← Previous Post
Next Post →

Related Posts

Laravel 5.8 CRUD (Create Read Update Delete) Tutorial For Beginners

Laravel

How to quickly generate data using faker and tinker ?

Laravel

How to create database seeder in Laravel 5.8 ?

Laravel

The Smart Way To Handle Request Validation In Laravel 5.8 (Laravel form validation)

Laravel

Recent Posts

  • Livewire Multiple Image Remove While UploadingFebruary 8, 2023
  • How to store multiple checkbox values in the database using Livewire?January 31, 2023
  • Accept only Gmail ID while registering in LivewireJanuary 12, 2023
  • Storage link already exists in LaravelDecember 31, 2022
  • Update Status using the toggle switch in LivewireDecember 29, 2022
  • Indian State, City SQL Database in LaravelDecember 23, 2022
  • JSON Encode and Decode in Livewire applicationDecember 21, 2022
  • Sweetalert2 delete confirmation in Livewire ExampleDecember 19, 2022
  • About
  • Portfolio
  • Privacy Policy
  • Terms and Conditions
  • Post
  • Contact

Copyright © 2022 8Bityard