public function getSocialHandle($provider) { $user = Socialite::driver($provider)->user(); $code = Input::get('code'); if (!$code) { return view('auth.login')->with('flash_message', 'You did not share your profile data with the social app.'); } if (!$user->email) { return view('auth.login')->with('flash_message', 'You did not share your email with the social app.'); } $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; $newSocialUser->name = $user->name; $newSocialUser->save(); $newSocialUser->assignDefaultRole(); $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; } } Auth::login($socialUser, true); $user = Auth::user(); $data = array('user' => $user); Mail::send('auth.emails.register-thank-you', $data, function ($message) use($user) { $recipient_email = $user->email; $recipient_name = $user->name; $subject = 'Welcome to Fitness Base!'; $message->to($recipient_email, $recipient_name)->subject($subject); }); if (Auth::user()->hasRole('user')) { return redirect('exercises'); } if (Auth::user()->hasRole('administrator')) { return redirect('exercises'); } return \App::abort(500); }