Laravel 5.2 : Do something after user has logged in?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel 5.2: Do Something After a User Has Logged In? Mastering Post-Authentication Hooks

Welcome to the world of application development! It’s completely normal to hit a wall when you start looking beyond the basic CRUD operations in a framework like Laravel. You've successfully set up authentication, which is a huge first step. Now you want to introduce dynamic behavior—actions that happen *after* key events occur, like a successful login or logout. This is where the real power of framework architecture comes into play.

As a developer progressing from beginner to senior, understanding how Laravel handles application flow—the "hooks" and "event listeners"—is crucial. In this post, we will explore the best ways to execute custom logic when users log in or log out in your Laravel application, moving beyond simple controller actions.


The Laravel Approach: Events and Listeners

You asked about hooks, event listeners, and event handlers—you are exactly on the right track! In Laravel, the Event System is the backbone for decoupling components. Instead of having your login logic directly handle session manipulation, you fire an event when a specific action happens (like successful authentication), and other parts of your application listen for that event to perform their tasks.

For complex applications, this system keeps your code clean, scalable, and easy to maintain. When you are building larger systems, remember that robust architecture, much like the principles discussed on Laravel documentation, relies heavily on these decoupled patterns.

Method 1: Intercepting Flow in Controllers (The Practical Start)

For simple session management tied directly to a specific request flow, the most straightforward method is often handled within your controller logic immediately following a successful authentication step. This is excellent for immediate, context-specific actions.

When a user successfully logs in, you handle the redirect and then perform the necessary session writes.

Example Scenario (Conceptual):

In your LoginController, after verifying credentials:

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

class LoginController extends Controller
{
    public function login(Request $request)
    {
        // 1. Authenticate the user (Assume this succeeds)
        if (Auth::attempt($request->only('email', 'password'))) {
            
            // --- ACTION AFTER LOGIN ---
            $user = Auth::user();

            // Set custom session data immediately upon successful login
            $request->session()->put('UserAgent', $request->server('HTTP_USER_AGENT'));
            $request->session()->put('loggedInAt', now());
            
            // Redirect to dashboard
            return redirect('/dashboard');
        }

        return redirect()->back()->withErrors(['message' => 'Invalid credentials.']);
    }
}

This method is direct and requires no extra setup, making it perfect for one-off actions like setting session variables upon login.

Method 2: Utilizing Event Listeners (The Scalable Solution)

For operations that need to be executed regardless of where the login occurs—for example, logging activity, updating external services, or performing complex data migrations—Events and Listeners are superior.

To implement this, you would first define an event, dispatch it from your controller, and then create a Listener class that handles the required logic.

Steps for Implementation:

  1. Define the Event: Create a new event class (e.g., UserLoggedIn).
  2. Create the Listener: Create a listener class that implements the ShouldListen interface and contains the logic to modify the session or database.
  3. Dispatch the Event: In your controller, dispatch the event after successful authentication:
// Inside your LoginController after Auth::attempt() succeeds:
event(new \App\Events\UserLoggedIn($user));
return redirect('/dashboard');
  1. Handle the Logic in the Listener: The listener receives the necessary data and performs the action:
// App/Listeners/UpdateSessionData.php
class UpdateSessionData
{
    public function handle(UserLoggedIn $event)
    {
        // This logic runs automatically after the event is dispatched
        $request = app('request'); // Accessing the request object contextually
        $request->session()->put('UserAgent', $request->server('HTTP_USER_AGENT'));
        \Log::info("User {$event->user->id} successfully logged in.");
    }
}

Conclusion

For a beginner using Laravel 5.2, start with Method 1 (Controller Logic) for immediate needs, as it is the quickest way to achieve your goal of setting session values upon login. As your application grows and you need complex, decoupled behavior—where multiple services might need to react to an event—transitioning to Method 2 (Events and Listeners) will provide you with a much more maintainable and professional architecture. Embrace the Event system; it is key to mastering Laravel development on the long term.