/** * Obtain the user information from GitHub. * * @param string $provider * * @return Response */ public function handleProviderCallback($provider) { // Check that provider exists as a service if (!Config::get('services.' . $provider)) { die('Provider ' . $provider . ' is not supported'); } if (Input::get('denied') != '') { die('You did not allow us to connect with your social profile on ' . $provider); } $user = Socialite::driver($provider)->stateless()->user(); $socialUser = null; //Check is this email present $userCheck = User::where('email', '=', $user->email)->first(); if (!empty($userCheck)) { $socialUser = $userCheck; } else { $sameSocialId = SocialLogin::where('social_id', '=', $user->id)->where('provider', '=', $provider)->first(); if (!empty($sameSocialId)) { $socialUser = $sameSocialId->user; } else { // There is no combination of this social id and provider, so we create new one $newSocialUser = new User(); $newSocialUser->email = $user->email; $newSocialUser->name = $user->name; $newSocialUser->password = bcrypt(str_random(16)); $newSocialUser->remember_token = str_random(64); $newSocialUser->api_token = str_random(60); $newSocialUser->save(); $socialData = new SocialLogin(); $socialData->social_id = $user->id; $socialData->provider = $provider; $newSocialUser->socialLogin()->save($socialData); $socialUser = $newSocialUser; } } event(new UserRegisteredThroughSocialite($socialUser)); Auth::login($socialUser, true); return Redirect::to('/'); }
/** * Deletes a social login entry from the database * * @param $id * @param $provider * @return \Spira\Responder\Response\ApiResponse */ public function unlinkSocialLogin($id, $provider) { if (!($socialLogin = SocialLogin::where('user_id', '=', $id)->where('provider', '=', $provider)->first())) { throw new NotFoundHttpException('Sorry, this provider does not exist for this user.'); } $socialLogin->delete(); /** @var \Tymon\JWTAuth\JWTAuth $jwtAuth */ $jwtAuth = App::make('Tymon\\JWTAuth\\JWTAuth'); $token = $jwtAuth->fromUser(User::find($id)); return $this->getResponse()->header('Authorization-Update', $token)->noContent(); }