I'm stuck on something which I'm pretty sure is quite basic.. but somehow I can't get it to work.
I created a middleware to set the locale of my website depending on the user's choice (his choice is registered in the database under 'locale' column). What I do is that I check is user is logged-in, if he is I use his locale, if not I default to English.
For some reason, my middleware never "sees" the user as logged-in and always defaults to English. I've output the result of Auth::check() to a log and indeed this always returns false, even if the user is actually logged-in.
I suppose this has to do with this Middleware being executed before the Auth one.. I've tried to change the priority in the Kernel file but that still doesn't work.. any idea?
My middleware
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class SetLocale {
public function handle($request, Closure $next) {
if(Auth::check()) {
$locale = Auth::user()->locale;
} else {
$locale = 'en';
}
\App::setLocale($locale);
return $next($request);
}
}
My route ('auth' is the default auth middleware, 'checkProjectPermission:read' checks if the user logged in has permission to access this project)
Route::prefix('client')->middleware(['auth','checkRoomPermission:read'])->group(function() {
Route::get('{project_id}/checklist', 'ProjectController@accessChecklist')->name('checklist');
}
My Kernel.php file :
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\SetLocale::class
];
protected $middlewarePriority = [
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Auth\Middleware\Authenticate::class,
\Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Auth\Middleware\Authorize::class,
\App\Http\Middleware\SetLocale::class
];
Thanks