setCurrentCompanyId($user->companies()->first()->account->default_company_id); } public function apiLogin(Request $request) { $this->validateLogin($request); if ($this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return response()->json(['message' => 'Too many login attempts, you are being throttled']); } if ($this->attemptLogin($request)) return $this->itemResponse($this->guard()->user()); else { $this->incrementLoginAttempts($request); return response()->json(['message' => ctrans('texts.invalid_credentials')]); } } /** * Redirect the user to the provider authentication page * * @return void */ public function redirectToProvider(string $provider) { if(request()->has('code')) return $this->handleProviderCallback($provider); else return Socialite::driver($provider)->redirect(); } /** * Received the returning object from the provider * which we will use to resolve the user, we return the response in JSON format * * @return json */ public function handleProviderCallbackApiUser(string $provider) { $socialite_user = Socialite::driver($provider)->stateless()->user(); if($user = OAuth::handleAuth($socialite_user, $provider)) { return $this->itemResponse($user); } else if(MultiDB::checkUserEmailExists($socialite_user->getEmail())) { return $this->errorResponse(['message'=>'User exists in system, but not with this authentication method'], 400); } /** 3. Automagically creating a new account here. */ else { //todo $name = OAuth::splitName($socialite_user->getName()); $new_account = [ 'first_name' => $name[0], 'last_name' => $name[1], 'password' => '', 'email' => $socialite_user->getEmail(), ]; $account = CreateAccount::dispatchNow($new_account); return $this->itemResponse($account->default_company->owner()); } } /** * We use this function when OAUTHING via the web interface * * @return redirect */ public function handleProviderCallback(string $provider) { $socialite_user = Socialite::driver($provider) ->scopes('https://www.googleapis.com/auth/gmail.send','email','profile','openid') ->stateless() ->user(); if($user = OAuth::handleAuth($socialite_user, $provider)) { Auth::login($user, true); return redirect($this->redirectTo); } else if(MultiDB::checkUserEmailExists($socialite_user->getEmail())) { Session::flash('error', 'User exists in system, but not with this authentication method'); //todo add translations return view('auth.login'); } /** 3. Automagically creating a new account here. */ else { //todo $name = OAuth::splitName($socialite_user->getName()); $new_account = [ 'first_name' => $name[0], 'last_name' => $name[1], 'password' => '', 'email' => $socialite_user->getEmail(), 'oauth_user_id' => $socialite_user->getId(), 'oauth_provider_id' => $provider ]; $account = CreateAccount::dispatchNow($new_account); Auth::login($account->default_company->owner(), true); return redirect($this->redirectTo); } } /** * A client side authentication has taken place. * We now digest the token and confirm authentication with * the authentication server, the correct user object * is returned to us here and we send back the correct * user object payload - or error. * * return User $user */ public function oauthApiLogin() { $user = false; $oauth = new OAuth(); $user = $oauth->getProvider(request()->input('provider'))->getTokenResponse(request()->input('token')); if ($user) return $this->itemResponse($user); else return $this->errorResponse(['message' => 'Invalid credentials'], 401); } }