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

kenshin9's avatar

Auth Basic - Setting Session Variables

I'm implementing an API so that clients can access our system. I'm currently using Auth Basic for authentication. Is there a way for me to set additional session variables when they authenticate this way?

There are session variables we usually set when they log in from the login page, and so there is functionality that depends on those values. So I'll need a way to set those same variables, but when using the Auth Basic middleware.

0 likes
1 reply
moharrum's avatar

The Auth Basic midlleware accepts a request as its first argument, you can use that to set you session:

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    return $this->auth->guard($guard)->basic() ?: $next($request);
}

try something like : $request->session()->set('name', 'value'); $request->session()->save();

Please or to participate in this conversation.