Symfony 5 login won’t authenticate or redirect

I am building a login page with Symfony 5 and am getting towards what is supposed to be the end of my journey to a functional one. However, whenever I enter credentials which I know are correct based on my DB, the login:

  1. Will not redirect to the root page (which, from what I understand, should be the default behavior after a successful login), but instead to the same empty login page, as though simply refreshing it
  2. Won’t even change my identity (I remain anonymous instead of becoming a registered user)

I already have a functional registration page. My goal is that login would work, a.k.a. show that I am a valid user in the bottom Symfony toolbar after I log in, and ideally also redirect somewhere, but hopefully this will come along with it and I won’t be disappointed if the former alone gets fixed. Here are relevant code snippets:

User class (to define a User and interact with DB):

<?php

namespace AppEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;

/**
 * User
 *
 * @ORMTable(name="user")
 * @ORMEntity
 * 
 * @UniqueEntity(
 *  fields = {"email"},
 *  message = "This e-mail is already in use"
 * )
 */
class User implements UserInterface
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="f_name", type="string", length=30, nullable=false)
     */
    private $fName;

    /**
     * @var string
     *
     * @ORMColumn(name="l_name", type="string", length=30, nullable=false)
     */
    private $lName;

    /**
     * @var string
     *
     * @ORMColumn(name="email", type="string", length=255, nullable=false)
     */
    private $email;

    /**
     * @var string
     *
     * @ORMColumn(name="department", type="string", length=40, nullable=false)
     */
    private $department;

    /**
     * @var string
     *
     * @ORMColumn(name="password", type="string", length=255, nullable=false)
     */
    private $password;

    public $confirmPassword;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFName(): ?string
    {
        return $this->fName;
    }

    public function setFName(string $fName): self
    {
        $this->fName= $fName;

        return $this;
    }

    public function getLName(): ?string
    {
        return $this->lName;
    }

    public function setLName(string $lName): self
    {
        $this->lName= $lName;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getDepartment(): ?string
    {
        return $this->department;
    }

    public function setDepartment(string $department): self
    {
        $this->department= $department;

        return $this;
    }

    public function getPassword(): ?string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password= $password;

        return $this;
    }

    public function eraseCredentials() {}
    public function getSalt() {}

    public function getRoles() {
        return ['ROLE_USER'];
    }
}

login.html.twig (to display the login page):

{% extends 'base.html.twig' %}

{% block body %}
<h1>Log In</h1>

<form action="{{ path('security_login') }}" method="post">
    <div class="form-group">
        <input type="text" class="form-control" name="_username" placeholder="E-mail" required/>
    </div>
    <div class="form-group">
        <input type="password" class="form-control" name="_password" placeholder="Password" required/>
    </div>
    
    <div class="form-group">
        <input type="submit" class="btn btn-primary" value="Log in"/>
    </div>
</form>

{% endblock %}

login() function in Controller:

/**
 * @Route("/login", name="security_login")
 */
public function login() {
    return $this->render('security/login.html.twig');
}

My security.yaml file, in the security folder:

security:
    encoders:
        AppEntityUser:
            algorithm: bcrypt

# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
    in_memory: { memory: ~ }
    in_database:
        entity:
            class: AppEntityUser
            property: email
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: true

        provider: in_database

        form_login:
            login_path: security_login
            check_path: security_login
        # activate different ways to authenticate
        # https://symfony.com/doc/current/security.html#firewalls-authentication

        # https://symfony.com/doc/current/security/impersonating_user.html
        # switch_user: true

# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
    # - { path: ^/admin, roles: ROLE_ADMIN }
    # - { path: ^/profile, roles: ROLE_USER }

I tried to translate all the names so that everything would be english, but my bad if something is wrong or missing.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *