redirect(); } public function providerCallback(String $provider) { try { $social_user = \Socialite::driver($provider)->user(); // First Find Social Account $account = SocialAccount::where([ 'provider_name' => $provider, 'provider_id' => $social_user->getId() ])->first(); // If Social Account Exist then Find User and Login if ($account) { auth()->login($account->user); return redirect('/studio/index'); } // Find User $user = User::where([ 'email' => $social_user->getEmail() ])->first(); // If User not found, then create new user if (!$user) { $user = User::create([ 'email' => $social_user->getEmail(), 'name' => $social_user->getName(), 'image' => $social_user->getAvatar(), 'littlelink_name' => $social_user->getNickname(), 'email_verified_at' => now(), ]); } // Create Social Accounts $user->socialAccounts()->create([ 'provider_id' => $social_user->getId(), 'provider_name' => $provider ]); // Login auth()->login($user); return redirect('/studio/index'); } catch (\Exception $e) { return redirect()->route('login')->withErrors($e->getMessage()); } } }