0
0
Laravelframework~5 mins

Remember me functionality in Laravel

Choose your learning style9 modes available
Introduction

The "Remember me" feature helps users stay logged in even after closing the browser. It saves time by not asking for login details every visit.

When users want to stay logged in on their personal device.
For websites where frequent login is annoying, like social media or forums.
On trusted devices to improve user experience.
When you want to reduce login friction but keep security reasonable.
Syntax
Laravel
Auth::attempt(['email' => $email, 'password' => $password], $remember);

The second parameter $remember is a boolean. Set it to true to enable "Remember me".

This method tries to log in the user and sets a long-lasting cookie if $remember is true.

Examples
This logs in the user and remembers them for future visits.
Laravel
$credentials = ['email' => $email, 'password' => $password];
Auth::attempt($credentials, true);
This logs in the user only for the current session (no remember me).
Laravel
$credentials = ['email' => $email, 'password' => $password];
Auth::attempt($credentials, false);
Sample Program

This controller method logs in a user using email and password. It checks if the "remember" checkbox is checked and passes that to Auth::attempt. If successful, it confirms login and whether "remember me" is active.

Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    public function login(Request $request)
    {
        $credentials = $request->only('email', 'password');
        $remember = $request->has('remember');

        if (Auth::attempt($credentials, $remember)) {
            return 'Logged in successfully with remember = ' . ($remember ? 'true' : 'false');
        }

        return 'Login failed';
    }
}
OutputSuccess
Important Notes

Make sure your login form has a checkbox named remember for this to work.

Laravel stores a secure cookie to remember the user, which lasts for 5 years by default.

Remember me should be used carefully on public or shared computers for security.

Summary

"Remember me" keeps users logged in across browser sessions.

Use Auth::attempt with a second boolean parameter to enable it.

Always balance convenience with security when using this feature.