Example #1
0
 /**
  * Provider used the "callback URL" and now we process the returned information
  */
 public function getSocialHandle(Request $request, $provider, AppMailer $mailer)
 {
     Log::info($request->ip() . ' - getSocialHandle - User gave consent to register using ' . $provider);
     $social = Socialite::driver($provider);
     $user = $social->user();
     $code = Input::get('code');
     if (!$code) {
         return redirect('login')->with('status', 'danger')->with('message', 'You did not share your profile data with c-SPOT.');
     }
     if (!$user->email) {
         return redirect('login')->with('status', 'danger')->with('message', 'You did not share your email with c-SPOT. You need to visit your ' . $provider . ' App Settings and remove c-SPOT, than you can come back here and login again. Or you can create a new account.');
     }
     $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)) {
             // As there is no combination of this social id and provider,
             // we create a new one
             $newSocialUser = new User();
             // the email address as provided by the service provider
             $newSocialUser->email = $user->email;
             // perhaps the email contains a name?
             $emailName = explode('@', $user->email)[0];
             if (strlen($user->name) < 3) {
                 $user->name = str_replace('.', ' ', $emailName);
             }
             // the name is hopefully a full name with first- and lastname
             $name = explode(' ', $user->name);
             $newSocialUser->first_name = $name[0];
             $newSocialUser->last_name = count($name) > 1 ? $name[1] : $name[0];
             // save the new user
             $newSocialUser->save();
             // Add role
             $role = Role::whereName('user')->first();
             $newSocialUser->assignRole($role);
             // create record in the social table
             $socialData = new Social();
             $socialData->social_id = $user->id;
             $socialData->provider = $provider;
             $newSocialUser->social()->save($socialData);
             $socialUser = $newSocialUser;
         } else {
             //Load this existing social user
             $socialUser = $sameSocialId->user;
         }
     }
     Log::info($request->ip() . ' - getSocialHandle - trying to do social-sign in');
     $mailer->notifyAdmin($socialUser, 'User confirmed via ' . $provider);
     // $this->auth->login($socialUser, true);
     Auth::login($socialUser, true);
     // write last login field in users table
     Auth::user()->update(['last_login' => Carbon::now()]);
     return redirect()->intended($this->redirectPath());
 }