public function authorize() { try { $this->accessToken = $this->helper->getAccessToken(); } catch (FacebookResponseException $e) { // When Graph returns an error throw new FacebookAuthenticationException('Graph returned an error: ' . $e->getMessage()); } catch (FacebookSDKException $e) { // When validation fails or other local issues throw new FacebookAuthenticationException('Facebook SDK returned an error: ' . $e->getMessage()); } if (!$this->accessToken) { throw new FacebookAuthenticationException('Access token not received. ' . $this->helper->getError(), $this->helper->getErrorCode()); } try { // Returns a `Facebook\FacebookResponse` object $response = $this->facebook->get('/me?fields=id,name', $this->accessToken); } catch (FacebookResponseException $e) { throw new FacebookAuthenticationException('Graph returned an error: ' . $e->getMessage()); } catch (FacebookSDKException $e) { throw new FacebookAuthenticationException('Facebook SDK returned an error: ' . $e->getMessage()); } $fbUser = $response->getGraphUser(); if (!($user = $this->doctrine->getRepository('QuizBundle:User')->findOneBySocialId($fbUser['id']))) { $user = (new Entity\User())->setAccessToken($this->accessToken)->setSocialType(Entity\User::FACEBOOK)->setSocialId($fbUser['id'])->setName($fbUser['name']); $manager = $this->doctrine->getManager(); $manager->persist($user); $manager->flush(); } $token = new SocialToken($user, $this->accessToken, 'facebook', [$this->adminId == $fbUser['id'] ? 'ROLE_ADMIN' : 'ROLE_USER']); $this->tokenStorage->setToken($token); }