Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1.  public function getSocialHandle($provider) {
  2.             $user = Socialite::driver($provider)->user();
  3.             $social_user = null;
  4.             //CHECK IF USERS EMAIL ADDRESS IS ALREADY IN DATABASE
  5.             $user_check = User::where('email', '=', $user->email)->first();
  6.             if (!empty($user_check)) {
  7.                 $social_user = $user_check;
  8.             } else { // USER IS NOT IN DATABASE BASED ON EMAIL ADDRESS
  9.                 $same_social_id = Social::where('social_id', '=', $user->id)->where('provider', '=', $provider)->first();
  10.                 // CHECK IF NEW SOCIAL MEDIA USER
  11.                 if (empty($same_social_id)) {
  12.                     $new_social_user = new User;
  13.                     $new_social_user->email = $user->email;
  14.                     $name = explode(' ', $user->name);
  15.                     if ($user->email) {
  16.                         $new_social_user->name = $user->email;
  17.                     } else {
  18.                         $new_social_user->name = $name[0];
  19.                     }
  20.                     $new_social_user->first_name = $name[0];
  21.                     // CHECK FOR LAST NAME
  22.                     if (isset($name[1])) {
  23.                         $new_social_user->last_name = $name[1];
  24.                     }
  25.                     $new_social_user->active = '1';
  26.                     $the_activation_code = str_random(60) . $user->email;
  27.                     $new_social_user->activation_code = $the_activation_code;
  28.                     // GET IP ADDRESS
  29.                     $userIpAddress = new CaptureIp;
  30.                     $new_social_user->signup_sm_ip_address = $userIpAddress->getClientIp();
  31.                     $new_social_user->save();
  32.                     $social_data = new Social;
  33.                     $social_data->social_id = $user->id;
  34.                     $social_data->provider = $provider;
  35.                     $new_social_user->social()->save($social_data);
  36.                     // ADD ROLE
  37.                     $role = Role::whereName('user')->first();
  38.                     $new_social_user->assignRole($role);
  39.                     // TODO: ADD LOGIC TO CAPTURE SOCIAL LOGIN USERNAME AND SAVE IT TO PROFILE
  40.                     $social_user = $new_social_user;
  41.                     // LINK TO PROFILE TABLE
  42.                     $profile = new Profile;
  43.                     $social_user->profile()->save($profile);
  44.                 } else {
  45.                     //Load this existing social user
  46.                     $social_user = $same_social_id->user;
  47.                 }
  48.             }
  49.             $this->auth->login($social_user, true);
  50.             if ($this->auth->user()->hasRole('user')) {
  51.                 //return redirect()->route('user.home');
  52.                 return redirect('app');
  53.             }
  54.             if ($this->auth->user()->hasRole('administrator')) {
  55.                 return redirect('app');
  56.                 //return redirect()->route('admin.home');
  57.             }
  58.             return \App::abort(500);
  59.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement