public function fbReceiveAuthorizationCodeAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $userService = $this->get('user_service');
     $userSession = $this->get('user_session');
     $src = $this->get('session')->get('fb_auth_src');
     $logger = $this->get('logger');
     $logger->info("FBAUTH: FB auth redirect");
     $helper = $this->getFBLoginHelper();
     try {
         $session = $helper->getSessionFromRedirect();
         if (!$session) {
             // Redirect to the signup page
             $logger->info("FBAUTH: FB auth denied by the user");
             return $this->redirect($this->generateUrl('signup'));
         }
         $fbRequest = new FacebookRequest($session, 'GET', '/me');
         $fbUser = $fbRequest->execute()->getGraphObject(GraphUser::className());
         $email = $fbUser->getEmail();
         if (!$email) {
             // TODO : Render error page
             $logger->error("FBAUTH: Email missing");
             echo "Email is required. Please revoke Class Central App from your <a href='https://www.facebook.com/settings?tab=applications'>Facebook settings page</a> and then signup again.";
             exit;
         }
         $name = $fbUser->getName();
         $fbId = $fbUser->getId();
         // Check if the fb users has logged in before using the FB Id
         $usersFB = $em->getRepository('ClassCentralSiteBundle:UserFb')->findOneBy(array('fbId' => $fbId));
         if ($usersFB) {
             $user = $usersFB->getUser();
         } else {
             // Check if an account with this email address exist. If it does then merge
             // these accounts
             $user = $em->getRepository('ClassCentralSiteBundle:User')->findOneBy(array('email' => $email));
         }
         if ($user) {
             $userService->login($user);
             $userSession->setPasswordLessLogin(true);
             // Check whether the user has fb details
             $ufb = $user->getFb();
             if ($ufb) {
                 $logger->info("FBAUTH: FB user exists");
             } else {
                 $logger->info("FBAUTH: Email exists but UserFb table is empty");
                 // Create a FB info
                 $ufb = new UserFb();
                 $ufb->setFbEmail($email);
                 $ufb->setFbId($fbId);
                 $ufb->setUser($user);
             }
             $em->persist($ufb);
             $em->flush();
             $userSession->login($user, true);
             $redirectUrl = $this->getLastAccessedPage($request->getSession()) ? $this->getLastAccessedPage($request->getSession()) : $this->generateUrl('user_library');
             $logger->info(' LOGIN REDIRECT URL ' . $redirectUrl);
             return $this->redirect($redirectUrl);
         } else {
             $logger->info("FBAUTH: New user");
             $newsletterService = $this->get('newsletter');
             $newsletter = $em->getRepository('ClassCentralSiteBundle:Newsletter')->findOneByCode('mooc-report');
             // Create a new account
             $user = new User();
             $user->setEmail($email);
             $user->setName($name);
             $user->setPassword($this->getRandomPassword());
             // Set a random password
             $user->setIsverified(true);
             $user->setSignupType(User::SIGNUP_TYPE_FACEBOOK);
             $redirectUrl = $userService->createUser($user, false, empty($src) ? 'facebook' : $src);
             $userSession->setPasswordLessLogin(true);
             // Set the variable to show that the user didn't use a password to login
             // Create a FB info
             $ufb = new UserFb();
             $ufb->setFbEmail($email);
             $ufb->setFbId($fbId);
             $ufb->setUser($user);
             $em->persist($ufb);
             $em->flush();
             $this->uploadFacebookProfilePic($user, $fbId);
             // Subscribe to newsletter
             $subscribed = $newsletterService->subscribeUser($newsletter, $user);
             $logger->info("preferences subscribed : email newsletter subscription", array('email' => $user->getId(), 'newsletter' => $newsletter->getCode(), 'subscribed' => $subscribed));
             return $this->redirect($redirectUrl);
         }
     } catch (FacebookRequestException $e) {
         $logger->info("FBAUTH: FB Auth error - " . $e->getMessage());
         return null;
     } catch (\Exception $e) {
         $logger->info("FBAUTH: Api exception" . $e->getMessage());
         return null;
     }
 }