Symfony 6 表单登录:键“email”必须是字符串,给出“NULL”

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

我在使用 Symfony 6.4、React、API Platform 和 LexikJWTAuthentication 时遇到以下错误。我已经使用 JWT 成功保护了我的 API,效果非常好。现在,我的目标是实现重定向到仪表板的用户登录。

但是,我面临一个问题:“键‘email’必须是一个字符串,给定‘NULL’。”

我已经在 security.yml 和 React 中配置了所有内容。我发现了类似的线程,但没有提供解决方案。有什么想法吗?

安全.yml:

security:
  enable_authenticator_manager: true
  password_hashers:
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
  providers:
    app_user_provider:
      entity:
        class: App\Entity\User
        property: email

  firewalls:
    dev:
      pattern: ^/{_{profiler|wdt}}/
      security: false

    login:
      pattern: ^/api/login
      stateless: true
      form_login:
        check_path: /api/login_check
        username_parameter: email
        password_parameter: password
        default_target_path: app_dashboard
        success_handler: lexik_jwt_authentication.handler.authentication_success
        failure_handler: lexik_jwt_authentication.handler.authentication_failure

    api:
      pattern:   ^/api/
      stateless: true
      jwt: ~

  access_control:
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }


when@test:
  security:
    password_hashers:
      Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
        algorithm: auto
        cost: 4
        time_cost: 3
        memory_cost: 10

lexik_jwt_authentication.yml

lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
    token_ttl: 3600

    api_platform:
        check_path: /api/login_check
        username_path: email
        password_path: security.credentials.password

登录.jsx

const Login = () => {
    const [email, setEmail] = useState("");
    const [password, setPassword] = useState("");

    const handleEmailChange = (e) => setEmail(e.target.value);
    const handlePasswordChange = (e) => setPassword(e.target.value);

    const handleSubmit = async (e) => {
        e.preventDefault();
        try {
            const response = await axios.post(
                'https://localhost/index.php/api/login_check',
                {email, password},
                {
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }
            );
            console.log('Login successful:', response.data);
        } catch (error) {
            console.error('Error during login:', error);
        }
    };
return (
    <form onSubmit={handleSubmit}>
        <Input
            type="email"
            variant="bordered"
            size="lg"
            label="Email"
            placeholder="Enter your email"
            labelPlacement="outside"
            className="pt-5 pb-5"
            value={email}
            id="email"
            name="_email"
            onChange={handleEmailChange}
        />
        <Input
            type="password"
            variant="bordered"
            size="lg"
            label="Password"
            placeholder="Enter your password"
            labelPlacement="outside"
            className="pt-5 pb-12"
            value={password}
            id="password"
            name="_password"
            onChange={handlePasswordChange}
        />
        <CustomButton label="Login" type="submit"/>
    </form>
)
            

有趣的是,请求中的电子邮件和密码根本不为空

{"email":"[email protected]","password":"test"}
reactjs symfony lexikjwtauthbundle
1个回答
0
投票

如果您使用 symfony 6.4,您应该在 security.yml 文件中使用此配置https://symfony.com/bundles/LexikJWTAuthenticationBundle/current/index.html#symfony-5-3-and-higher。此外,在您的代码中: 标题:{ '内容类型':'应用程序/json', }, 您使用“application/json”,您应该检查 config/packages/api_platform.yaml 中的文件。

api_平台: 标题:Hello API 平台 版本:1.0.0 格式: jsonld: ['应用程序/ld+json'] 文档格式: jsonld: ['应用程序/ld+json'] jsonopenapi: ['application/vnd.openapi+json'] html: ['文本/html'] 默认值: 无状态:正确 缓存头: 变化:['内容类型','授权','来源'] 额外属性: 标准输入:真 rfc_7807_compliance_errors:真 event_listeners_backward_compatibility_layer: false keep_legacy_inflector: false 查看你的api接受的格式是否是json而不是ld+json。

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