cookieService = $cookieService; $this->apiService = $apiService; } public function handle(Request $request, Closure $next) { $token = $this->cookieService->getCookie('TOKEN'); $publicRoutes = ['/', '/login', '/registration', '/change-password', '/topup-success-page', '/topup-error-page']; if ($token) { try { $response = $this->apiService->post('adminProfile'); if ($response) { Session::put('userInfo', $response); Session::put('isAuthenticated', true); if (in_array($request->path(), $publicRoutes)) { return redirect('/user-management'); } } } catch (\Exception $e) { Log::error('Failed to load user data', ['error' => $e->getMessage(), 'source' => 'CheckAuth']); Session::flash('error', 'Something went wrong loading user data.'); } } return $next($request); } }