部署中的Facebook身份验证失败

时间:2019-09-18 15:42:25

标签: facebook symfony heroku

iam实现了Facebook API的身份验证,并且在localhost上运行良好 但是当我将代码推送到远程并部署时不起作用
所以我强调文字尝试检查Heroku的日志

  

在=信息方法= GET路径= “/连接/校验码= AQDtr28nAyPB2NaVcSqiujqvMXF_FWZsi0LJNb - ?v6U_fNviQiM1Ap-zW0F2N3ssXraV8GLpvCNKpkohPeYCyHJP5C7RGQZgX4o4P4NLTmGPAXdxddBnwaD97GRcryPWzjZS_rsSbIszDjph-qLGuBuTtkGWfKFfbw8X_BxY24hdHrGcu04fZCUlIeZ49SdFoOyVCS1pQ8E6T9jTutcGwIKJzqInL5yn0RPN8eEoYCF8tF26R7E2WkgWacGAd0r5r7LXG9eB7SWn_ff0cQVgqWTA2V7zazuExPrtJHhvhIRC57maut71XW45o2kf9_AoNGQ22hK3FRlBAyg6YKmrCO2s&状态= f97fdd099040707dccad41924aa6e5bd” 宿主= still-reef-88809.herokuapp.com REQUEST_ID = 883af12c-d4c6-4211-ADF4 -632e7237afce fwd =“ 41.140.214.3” dyno = web.1 connect = 0ms服务= 74ms状态= 302字节= 512协议= https

这是Facebook身份验证器

class FacebookAuthenticator extends SocialAuthenticator
{

private $clientRegistry;

/**
 * @var EntityManagerInterface
 */
private $em;
private $router;
private $urlgenerator;
private $security;

/**
 * FacebookAuthenticator constructor.
 * @param ClientRegistry $clientRegistry
 * @param EntityManagerInterface $em
 */
public function __construct(ClientRegistry $clientRegistry, EntityManagerInterface $em ,RouterInterface $router,UrlGeneratorInterface $urlgenerator,Security $security)
{
    $this->clientRegistry = $clientRegistry;
    $this->em = $em;
    $this->router = $router;
    $this->urlgenerator= $urlgenerator;
    $this->security =$security;

}

/**
 * @param Request $request
 * @return bool
 */
public function supports(Request $request)
{
    // // continue ONLY if the current ROUTE matches the check ROUTE
    // return $request->attributes->get('_route') === 'connect_facebook_check';
    return $request->getPathInfo() == '/connect/check' && $request->isMethod('GET');
}

/**
 * @param Request $request
 * @return \League\OAuth2\Client\Token\AccessToken|mixed
 */
public function getCredentials(Request $request)
{
    // this method is only called if supports() returns true

    return $this->fetchAccessToken($this->getFacebookClient());
}

/**
 * @param mixed $credentials
 * @param UserProviderInterface $userProvider
 * @return User|null|object|\Symfony\Component\Security\Core\User\UserInterface
 */
public function getUser($credentials, UserProviderInterface $userProvider)
{
    /** @var FacebookUser $facebookUser */
    $facebookUser = $this->getFacebookClient()
        ->fetchUserFromToken($credentials);

    $email = $facebookUser->getEmail();
    $user  = $this->em->getRepository(User::class)->findOneBy(['email' => $email]);

    if(!$user){
    throw new CustomUser;
    }

    return $user;
    // $facebookUser = $this->getFacebookClient();



}

/**
 * @return FacebookClient
 */
private function getFacebookClient()
{
    return $this->clientRegistry->getClient('facebook_main');
}

/**
 * @param Request $request
 * @param TokenInterface $token
 * @param string $providerKey
 * @return null|Response
 */
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    return new RedirectResponse($this->urlgenerator->generate('home')); 
}

/**
 * @param Request $request
 * @param AuthenticationException $exception
 * @return null|Response
 */
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    return new RedirectResponse($this->urlgenerator->generate('home'));
}

/**
 * Called when authentication is needed, but it's not sent.
 * This redirects to the 'login'.
 *
 * @param Request $request
 * @param AuthenticationException|null $authException
 *
 * @return RedirectResponse
 */
public function start(Request $request, AuthenticationException $authException = null)
{
    return new RedirectResponse($this->urlgenerator->generate('app_login'));
}
}

并且是facebook控制器:

class FacebookController extends AbstractController

{

  /**
     * Link to this controller to start the "connect" process
     *
     * @Route("/connect/facebook",name="connect_facebook" )
     */
    public function connectAction(ClientRegistry $clientRegistry)
    {
        return $clientRegistry
            ->getClient('facebook_main')
            ->redirect();   
    }
    /**
     * Facebook redirects to back here afterwards
     *
     * @Route("/connect/check", name="connect_facebook_check")
     */
    public function connectCheckAction(Request $request)
    {
        if ($this->getUser() ==null) {
            return new JsonResponse(array('status' => false, 'message' => "User not found!"));
        }
        else {

            return $this->redirectToRoute('home');

    }   

    }
// /**
//  *@Route("facebook/logout",name="logoutFacebook")
//  */
// public function logoutFacebook(ClientRegistry $clientRegistry){
//    $clientRegistry->getLogoutUrl();
//    return $this->redirectToRoute('home');
// }

}

不是登录用户,知道已成功在本地主机上登录 最好的问候

0 个答案:

没有答案