gpt4 book ai didi

php - 获取经过身份验证的用户 onAuthenticationSuccess

转载 作者:行者123 更新时间:2023-12-01 10:51:43 27 4
gpt4 key购买 nike

我有以下身份验证处理程序:

class LoginAuthSuccessHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
private $router;
private $container;

/**
* Constructor
* @param RouterInterface $router
*/
public function __construct(RouterInterface $router, $container)
{
$this->router = $router;
$this->container = $container;
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($request->isXmlHttpRequest()) {
$user = $this->container->get('security.context')->getToken()->getUser();
$result = array('success' => true, 'user' => $user);
return new Response(json_encode($result));
} else {
$route = $this->router->generate('ShopiousMainBundle_profile');

$referrer_url = $request->server->get('HTTP_REFERER');
if (strstr($referrer_url, '/items/')) {
$route = $referrer_url;
}

return new RedirectResponse($route);
}
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
if ($request->isXmlHttpRequest()) {
$result = array('success' => false, 'message' => $exception->getMessage());
return new Response(json_encode($result));
} else {
// Handle non XmlHttp request here
}
}
}

为什么是这样:
$user =  $this->container->get('security.context')->getToken()->getUser();

返回空?此时我如何获得经过身份验证的用户?

最佳答案

您应该使用 $token您作为参数接收的变量而不是 $this->container->get('security.context')->getToken() .

$user = $token->getUser();

关于php - 获取经过身份验证的用户 onAuthenticationSuccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18535905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com