with('client.gateway_tokens','company') ->where('email', auth('contact')->user()->email) ->whereNotNull('email') ->where('email', '<>', '') ->whereNull('deleted_at') ->distinct('company_id') ->distinct('email') ->whereNotNull('company_id') ->whereHas('client', function ($query) { return $query->where('is_deleted', false); }) ->whereHas('company', function ($query){ return $query->where('account_id', auth('contact')->user()->client->company->account->id); }) ->get(); /* This catches deleted clients who don't have access to the app. We automatically log them out here*/ if (count($multiple_contacts) == 0) { Auth::logout(); return redirect()->route('client.login')->with('message', 'Login disabled'); } if (count($multiple_contacts) == 1 && !Auth::guard('contact')->check()) { Auth::guard('contact')->loginUsingId($multiple_contacts[0]->id, true); } session()->put('multiple_contacts', $multiple_contacts); return $next($request); } }