Session is started by a middleware. Put your middleware after the session middleware.
May 11, 2016
4
Level 2
Session variables unknown during building of menu middleware
I want my webmaster to be able to impersonate as another user. Therefore I have a form with a controller to write a session variable containing the ID he is impersonating. In my authentication middleware I check for the presence of this session variable:
class AuthenticateOnceWithBasicAuth {
public function handle($request, Closure $next) {
if (Session::has('simulate_as')) {
$username = Session::get('simulate_as');
}
else {
$username = $_SERVER["REMOTE_USER"]; // fallback on single login authentication
}
$user = User::where('username', $username)->first();
Auth::login($user);
return $next($request);
}
}
Problem is that when using the menu middleware, the session is not yet sent to the browser. Should I change something in the order of my items in kernel.php?
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\AuthenticateOnceWithBasicAuth::class,
\App\Http\Middleware\MenuMiddleware::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
Level 56
Please or to participate in this conversation.