首页 > 解决方案 > 如果我扩展 AbstractFormLoginAuthenticator,如何在 Symfony4 中使用电子邮件而不是用户名?

问题描述

我的email字段中有字段,login.html.twig并且想将最后输入email的,而不是用户名传递给我的模板。如何获取email表单 AuthenticationUtils::getLastUsername()?

标签: phpauthenticationsymfony4

解决方案


只需传递给类的方法内的email会话:getCredentials()LoginAuthenticator

public function getCredentials(Request $request)
{
    $credentials = [
        'email' => $request->request->get('email'),
        'password' => $request->request->get('password')
    ];

    $request->getSession()->set(
        Security::LAST_USERNAME,
        $credentials['email']
    );

    return $credentials;
}

推荐阅读