Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adnanerlansyah403's avatar

How to forget specific session when user's login with authentication from Jetstream

Hello everyone, I have a question about how to forget a specific session when user's login. So where's default controller that managed for user login. For example I want remove this session after user's login

Session::forget('coupon_applied')

And thank you so much for all πŸ™

0 likes
4 replies
MohamedTammam's avatar

Under the hood Jetstream using Fortify.

You can customize the authentication method in JetstreamServiceProvider

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    // ...

    Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)->first();

        if ($user &&
            Hash::check($request->password, $user->password)) {
			Session::forget('coupon_applied');
            return $user;
        }
    });
}

https://jetstream.laravel.com/2.x/features/authentication.html#customizing-user-authentication

Niush's avatar
Niush
Best Answer
Level 50

Alternatively, instead of using authenticateUsing (which does not handle throttle, 2fa etc.) you can customize Fortify login pipeline as below:

https://jetstream.laravel.com/2.x/features/authentication.html#customizing-the-authentication-pipeline

// JetstreamServiceProvider.php
use App\Actions\DoSomeThingMore;
use Illuminate\Http\Request;
use Laravel\Fortify\Actions\AttemptToAuthenticate;
use Laravel\Fortify\Actions\EnsureLoginIsNotThrottled;
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use Laravel\Fortify\Actions\RedirectIfTwoFactorAuthenticatable;
use Laravel\Fortify\Fortify;

// ...

public function boot()
{
  // ...

  Fortify::authenticateThrough(function (Request $request) {
      return array_filter([
          config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
          RedirectIfTwoFactorAuthenticatable::class,
          AttemptToAuthenticate::class,
          PrepareAuthenticatedSession::class,
          DoSomeThingMore::class
      ]);
  });
}

And, the DoSomeThingMore class could have something like this:

<?php

namespace App\Actions;

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

class DoSomeThingMore
{
    /**
     * Do Something Here the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  callable  $next
     * @return callable $next
     */
    public function __invoke(Request $request, $next)
    {
        $request->session()->forget('coupon_applied');
        Log::info(auth()->user());
        return $next($request);
    }
}
1 like

Please or to participate in this conversation.