Example #1
0
 public function facebookCallback()
 {
     $user = Socialite::driver('facebook')->user();
     $provider = 'facebook';
     if (Auth::user()) {
         $localUser = Auth::user();
         if ($localUser->profile) {
             UserMapper::confirmFacebook(Auth::user(), $user);
         }
         return redirect('/profile');
     }
     $oauthId = $user->getId();
     $localUser = User::oauth($oauthId, $provider)->first();
     if ($localUser) {
         Auth::loginUsingId($localUser->id);
         return redirect($this->redirectPath);
     } else {
         //dd($user);
         $nickname = $user->getNickname();
         $name = $user->getName();
         $email = $user->getEmail();
         $avatar = $user->getAvatar();
         $firstName = $user->offsetGet('last_name');
         $lastName = $user->offsetGet('first_name');
         $userType = Session::pull('register.user_type');
         $type = Session::pull('register.type');
         if (!$userType) {
             $userType = null;
         }
         if (!$type) {
             $type = null;
         }
         $data = ['name' => $name, 'first_name' => $firstName, 'last_name' => $lastName, 'email' => $email, 'password' => '', 'city' => null, 'mobile' => null, 'user_type' => $userType, 'type' => $type];
         $localUser = $this->create($data);
         $localUser->provider = $provider;
         $localUser->oauth_id = $oauthId;
         if ($nickname == '') {
             $nickname = $oauthId;
         }
         $profile = $localUser->profile;
         $profile->facebook = 'http://facebook.com/' . $nickname;
         $profile->confirmed_facebook = true;
         $localUser->save();
         $avatarPath = UserMapper::generateAvatarPath($localUser);
         try {
             if ($avatar) {
                 file_put_contents($avatarPath, fopen($avatar, 'r'));
                 $profile->avatar = pathinfo($avatarPath, PATHINFO_BASENAME);
             }
         } catch (\Exception $e) {
             $profile->avatar = null;
         }
         $profile->save();
         Auth::loginUsingId($localUser->id);
         if ($user) {
             return redirect($this->redirectPath);
         } else {
             return Redirect::back()->withErrors(['facebook' => trans('register.facebook.fail')]);
         }
     }
 }
 public function saveAvatarFromBase64($base64str)
 {
     list($format, $base64str) = explode(';', $base64str);
     list($encoding, $base64str) = explode(',', $base64str);
     if ($encoding != 'base64') {
         return false;
     }
     $data = base64_decode($base64str);
     if (strpos($format, 'png') !== false) {
         $format = 'png';
     }
     if (strpos($format, 'jpg') !== false || strpos($format, 'jpeg') !== false) {
         $format = 'jpg';
     }
     if (strpos($format, 'gif') !== false) {
         $format = 'gif';
     }
     $avatarPath = UserMapper::generateAvatarPath($this->user, $format);
     if (file_put_contents($avatarPath, $data)) {
         return $avatarPath;
     }
     return false;
 }