Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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 :
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 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
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
<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().
<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.
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.
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.