Search Here

Implement Laravel Remember Me Functionality in Your Web Application

Implement Laravel Remember Me Functionality in Your Web Application

In this tutorial, you’ll learn to implement remember me functionality in your Laravel web application.

What is Remember Me?

The Remember Me feature allows the client-side users to automatically remember their user login details as they regularly visit the website. In most cases, the user login information is store in the form of a cookie.

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 to find in Mozilla Firefox goto inspect element by right-clicking mouse and after clicking on inspect element goto storage tab there you will find cookies used by your laravel site under option cookies.

How to implement remember Me functionality in your Laravel Application

If you take a look at official laravel documentation then there is clearly mentioned then you can set the browser to remember user by passing a boolean value as the second argument to Auth::attempt($credentials, $remember_me). The default value of remembers me argument is set to FALSE.

In users table, you’ll also find a remember_token column which contains encrypted string on user information.

Here is the main snippet for remembering user.

// In LoginController.php
$credential = [
    'email' => $request->email,
    'password' => $request->password,
];

$remember_me  = ( !empty( $request->remember_me ) )? TRUE : FALSE;

if(Auth::attempt($credential)){
    $user = User::where(["email" => $credential['email']])->first();
    
    Auth::login($user, $remember_me);

    // redirect authenticated user to another page
}

Here the $request->remember_me gets value from a checkbox and if user checks on checkbox than remember_me will have a boolean value TRUE else FALSE.

Udemy Laravel Course for Beginners

Udemy Laravel Course for Beginners

Complete Source Code:

User Model

<?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 = [
        'first_name', 'last_name', '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',
    ];
}

The above is the snippet of the default user model provided by Laravel out of the box.

Web Routes

// Remember Me routes
Route::get('remember-me/login', 'LoginController@index')->name('remember-me.login');
Route::post('remember-me/login/verify', 'LoginController@verify')->name('remember-me.login-verify');
Route::get('remember-me/logout', 'LoginController@logout')->name('remember-me.logout');

// User will be redirected to this route after successful authentication
Route::get('remember-me/dashboard', 'DashboardController@index')->name('dashboard');

Here are the below routes and purpose they serve:

  • remember-me.login: Takes the user to login page
  • remember-me.login-verify: Validate the user trying to log in and also sets the remember-me cookie if the remember-me checkbox is selected.
  • remember-me.logout: Logs out the authenticated user and clears his session.
  • dashboard: Takes the user to dashboard page after successful login/

Login LoginController

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    public function index(Request $request){
        $info = [];
        return view('remember_me.login', $info);
    }

    public function verify(Request $request){
        $credential = [
            'email' => $request->email,
            'password' => $request->password,
        ];

        $remember_me  = ( !empty( $request->remember_me ) )? TRUE : FALSE;

        if(Auth::attempt($credential)){
            $user = User::where(["email" => $credential['email']])->first();
            
            Auth::login($user, $remember_me);

            return redirect(route('dashboard'));
        }
    }

    public function logout(Request $request){
        Auth::logout();
        return redirect(route('remember-me.login'));
    }
}

Here Auth::login($user, $remember_me); determines where to set the remember-me cookie.

View: remember_me/login.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel | Implement Remember Me Functionality</title>
    
    <link rel="stylesheet" href="{{ url('assets/css/bootstrap.min.css') }}">

    <style>
        .error{
            color: red;
        }
    </style>
</head>
<body>

    
    <div class="container">
        <div class="col-md-3 offset-4">
            <h2 class="text-center" >Login</h2>

            <div class="">
                <form action="{{ route('remember-me.login-verify') }}" method="post">
                    @csrf
                    <div>
                        <label for="">Email</label>
                        <input type="text" class="form-control" name="email" value="john123@gmail.com">
                    </div>

                    <div>
                        <label for="">Password</label>
                        <input type="password" class="form-control" name="password" value="123456">
                    </div>

                    <div>
                        <br>
                        <label for="">Remember Me</label>
                        <input type="checkbox" name="remember_me" value="1">
                    </div>

                    <div>
                        <input type="submit" class="btn btn-primary" value="Login">
                    </div>
                </form>
            </div>
        </div>
    </div>

</body>
</html>

View: remember_me/dashboard.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
</head>
<body>
    <h2>Dashboard Page</h2>

    <a href="{{ route('remember-me.logout') }}">Logout</a>
</body>
</html>

Conclusion

In Conclusion, In the end, you have learnt to Implement Laravel Remember Me Functionality in Your Web Application. For the tutorial, suggestions contact us and comment if you have any queries.

Related Posts

Summary
Review Date
Reviewed Item
Implement Laravel Remember Me Functionality in Your Web Application
Author Rating
51star1star1star1star1star
Software Name
Laravel Web Development
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development