/** * Obtain the user information from the social media site. * * @return Response */ public function Callback($provider) { $user = Socialite::driver($provider)->user(); $socialUser = null; //Check is this email present $userCheck = User::where('email', '=', $user->email)->first(); if (!empty($userCheck)) { $socialUser = $userCheck; } else { $sameSocialId = Social::where('social_id', '=', $user->id)->where('provider', '=', $provider)->first(); if (empty($sameSocialId)) { //There is no combination of this social id and provider, so create new one $name = explode(' ', $user->name); $data = ['first_name' => $name[0], 'last_name' => $name[1], 'email' => $email]; $this->userRepository->SocialRegister($newSocialUser); $socialData['social_id'] = $user->id; $socialData['provider'] = $provider; $socialRepository->saveSocial($socialData); $role = Role::whereName('user')->first(); $newSocialUser->assignRole($role); $socialUser = $newSocialUser; } else { //Load this existing social user $socialUser = $sameSocialId->user; } } $this->auth->login($socialUser, true); if ($this->auth->user()->hasRole('user')) { $this->checkFutureList(); return redirect('check'); } if ($this->auth->user()->hasRole('administrator')) { return redirect('adminHome'); } }
public function getSocialHandle($provider) { $user = Socialite::with($provider)->user(); $socialUser = null; //Check is this email present $userCheck = User::where('email', '=', $user->email)->first(); if (!empty($userCheck)) { $socialUser = $userCheck; } else { $sameSocialId = Social::where('social_id', '=', $user->id)->where('provider', '=', $provider)->first(); if (empty($sameSocialId)) { //There is no combination of this social id and provider, so create new one $newSocialUser = new User(); $newSocialUser->email = $user->email; $name = explode(' ', $user->name); $newSocialUser->first_name = $name[0]; $newSocialUser->last_name = $name[1]; $newSocialUser->save(); $socialData = new Social(); $socialData->social_id = $user->id; $socialData->provider = $provider; $newSocialUser->social()->save($socialData); // Add role $role = Role::whereName('user')->first(); $newSocialUser->assignRole($role); $socialUser = $newSocialUser; } else { //Load this existing social user $socialUser = $sameSocialId->user; } } $this->auth->login($socialUser, true); if ($this->auth->user()->hasRole('user')) { return redirect()->route('/'); } if ($this->auth->user()->hasRole('administrator')) { return redirect()->route('/backoffice'); } return \App::abort(500); }