键“_username”必须是字符串,给定“NULL”

问题描述 投票:0回答:1

提交登录表单后,我收到“密钥“_username”必须是字符串,给出“NULL””

这是我的jsx:

fetch('/signin', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: JSON.stringify({
        _username: "[email protected]", // Test email
        _password: "your_password" // Test password
    })
})
.then((res) => res.json())

和我的控制器:

#[Route('/signin', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
    if ($this->getUser()) {
        return $this->redirectToRoute('app_dashboard');
    }


    $error = $authenticationUtils->getLastAuthenticationError();
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->json([
        'last_username' => $lastUsername,
        'error' => $error ? $error->getMessage() : null,
    ]

和我的 security.yaml

        form_login:
            login_path: /signin
            check_path: /signin
            enable_csrf: true
            secure: auto

你能帮我吗

symfony security
1个回答
0
投票

您只需使用 JSON 登录即可使用 API 进行登录,

https://symfony.com/doc/current/security.html#json-login

© www.soinside.com 2019 - 2024. All rights reserved.