/** * Process login with Facebook * * @param \Facebook\GraphNodes\GraphUser $userNode * @return array */ private function _process($userNode) { $userInfo = []; $userInfo['email'] = $userNode->getField('email'); $userInfo['first_name'] = $userNode->getFirstName(); $userInfo['last_name'] = $userNode->getLastName(); $userInfo['facebook_id'] = $userNode->getId(); return (new ZSocialHelper($userInfo, 'facebook'))->process(); }
public function testMeReturnsGraphUser() { $response = (new FacebookRequest(FacebookTestHelper::$testSession, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className()); $info = FacebookTestHelper::$testSession->getSessionInfo(); $this->assertTrue($response instanceof GraphUser); $this->assertEquals($info->getId(), $response->getId()); $this->assertNotNull($response->getName()); $this->assertNotNull($response->getLastName()); $this->assertNotNull($response->getLink()); }
public function testAsList() { $backingData = array('data' => array(array('id' => 1, 'name' => 'David'), array('id' => 2, 'name' => 'Fosco'))); $enc = json_encode($backingData); $response = new FacebookResponse(null, json_decode($enc), $enc); $list = $response->getGraphObjectList(GraphUser::className()); $this->assertEquals(2, count($list)); $this->assertTrue($list[0] instanceof GraphObject); $this->assertTrue($list[1] instanceof GraphObject); $this->assertEquals('David', $list[0]->getName()); $this->assertEquals('Fosco', $list[1]->getName()); }
/** * Register a user from a GraphUser object. * * @param GraphUser $userGraph * * @return integer|boolean */ public function register(GraphUser $userGraph) { $username = sanitize_user($userGraph->getFirstName(), true); $i = ''; while (username_exists($username . $i)) { $i = absint($i); $i++; } $username = $username . $i; $userdata = apply_filters($this->container->getSlug() . '.register.user.filter', array('user_pass' => wp_generate_password(), 'user_login' => $username, 'user_nicename' => $username, 'user_email' => $userGraph->getField('email'), 'display_name' => $userGraph->getField('name'), 'nickname' => $username, 'first_name' => $userGraph->getField('first_name'), 'last_name' => $userGraph->getField('last_name'), 'role' => get_option('default_role'))); $localUserId = wp_insert_user($userdata); if ($localUserId instanceof WP_Error) { throw new Exception($localUserId->get_error_message(), 500); } return $localUserId; }
/** * Build new User Model from Facebook GraphUser object * * @param GraphUser $user * * @return App\User $new */ protected function validateUserData(GraphUser $user) { $new = new User(); $new->facebook_id = $user->getId(); $new->name = $user->getName(); $new->avatar = $user->getPicture()->getUrl(); $new->last_login = Carbon::now()->toDayDateTimeString(); if (is_null($user->getEmail())) { $new->email = 'No Email'; } else { $new->email = $user->getEmail(); } if (is_null($user->getLocation())) { $new->location = 'No location'; } else { $new->location = $user->getLocation()->getName(); } return $new; }
/** * Create or update user by using data from facebook and login to store * * @param GraphUser $facebookUser * @param AccessToken $accessToken * * @return CustomerInterface */ private function createOrUpdateAndLogin(GraphUser $facebookUser, AccessToken $accessToken) { if (!$this->customer->getId()) { $this->customer->setEmail($facebookUser->getEmail()); $this->customer->setFirstname($facebookUser->getFirstName()); $this->customer->setLastname($facebookUser->getLastName()); $this->customer->setMiddlename($facebookUser->getMiddleName()); $this->customer->setGender((int) ($facebookUser->getGender() == 'male')); } $this->customer->setCustomAttribute('sf_id', $facebookUser->getId()); $this->customer->setCustomAttribute('sf_access_token', serialize($accessToken)); if ($this->customer->getId()) { $customer = $this->customerRepository->save($this->customer); } else { $customer = $this->accountManagement->createAccount($this->customer); } $this->login($customer->getId()); return $customer; }
public function getUser($userId = 'me') { try { $request = new FacebookRequest($this->getSession(), 'GET', '/' . $userId); return $request->execute()->getGraphObject(GraphUser::className())->asArray(); } catch (Exception $e) { } return []; }
/** * Get the Facebook account profile link * * @return string url */ public function getProfileUrl() { return $this->graphuser->getLink(); }
session_start(); require 'config/facebook.php'; require 'vendor/autoload.php'; use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; use Facebook\FacebookResponse; use Facebook\GraphUser; use Facebook\GraphObject; use Facebook\FacebookRequestException; FacebookSession::setDefaultApplication($config['app_id'], $config['app_secret']); $helper = new FacebookRedirectLoginHelper('http://localhost/subastaloDotCom/home/'); try { $session = $helper->getSessionFromRedirect(); if ($session) { $_SESSION['facebook'] = $session->getToken(); header('Location: index.php'); } if (isset($_SESSION['facebook'])) { $session = new FacebookSession($_SESSION['facebook']); $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $graphObjectClass = $response->getGraphObject(\Facebook\GraphNodes\GraphUser::className()); $facebook_User = $graphObjectClass; } } catch (FacebookRequestException $ex) { // When Facebook returns an error } catch (\Exception $ex) { // When validation fails or other local issues }