public function execute($provider, $hasCode)
{
if (!$hasCode) {
return $this->getAuthorization($provider);
}
$socialiteUser = Socialite::driver($provider)->user();
$providerId = AuthService::getProviderId($provider);
if (Auth::check()) {
$user = Auth::user();
$isRegistered = $user->registered;
$email = $socialiteUser->email;
$oauthUserId = $socialiteUser->id;
$name = Utils::splitName($socialiteUser->name);
$result = $this->accountRepo->updateUserFromOauth($user, $name[0], $name[1], $email, $providerId, $oauthUserId);
if ($result === true) {
if (!$isRegistered) {
event(new UserSignedUp());
Session::flash('warning', trans('texts.success_message'));
Session::flash('onReady', 'handleSignedUp();');
} else {
Session::flash('message', trans('texts.updated_settings'));
return redirect()->to('/settings/' . ACCOUNT_USER_DETAILS);
}
} else {
Session::flash('error', $result);
}
} else {
if ($user = $this->accountRepo->findUserByOauth($providerId, $socialiteUser->id)) {
Auth::login($user, true);
event(new UserLoggedIn());
} else {
Session::flash('error', trans('texts.invalid_credentials'));
return redirect()->to('login');
}
}
$redirectTo = Input::get('redirect_to') ?: 'dashboard';
return redirect()->to($redirectTo);
}