'Symfony 5 : Authentication failed in prod env

I have an application that I'm trying to upload to my FTP server.

I've run : symfony console make:user and also symfony console make:auth So while running the server locally with symfony serve I have no problem loging in with the files that symfony has created automatically. I did not change anything but when I uploaded the project to my ftp (drag & drop using Cyberduck), it doesn't allow me to log in, it doesn't even display an error even if i'm using wrong credentials.. it just refreshes the page.. I don't know if it has any effect on this problem but i've also ran composer require symfony/apache-pack before the upload but my login is still working locally even with this require

In my Authenticator class (the file generated inside the src/Security/ folder by the make:auth command) i've changed the authenticate() function to add a dd() just to see if it is even called, and the result are :

  • locally : the dd() is called
  • in my FTP : the dd() is not..

Here is my full Authenticator class :

<?php

namespace App\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

class Authenticator extends AbstractLoginFormAuthenticator
{
    use TargetPathTrait;

    public const LOGIN_ROUTE = 'app_login';

    private $urlGenerator;

    public function __construct(UrlGeneratorInterface $urlGenerator)
    {
        $this->urlGenerator = $urlGenerator;
    }

    public function authenticate(Request $request): PassportInterface
    {
        $username = $request->request->get('username', '');
        $request->getSession()->set(Security::LAST_USERNAME, $username);
        $passeport = new Passport(
            new UserBadge($username),
            new PasswordCredentials($request->request->get('password', '')),
            [
                new CsrfTokenBadge('authenticate', $request->get('_csrf_token')),
            ]
        );
        dd($passeport);
        return $passeport;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
    {
        if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
            return new RedirectResponse($targetPath);
        }

        // For example:
        return new RedirectResponse($this->urlGenerator->generate('dashboard'));
        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
    }

    protected function getLoginUrl(Request $request): string
    {
        return $this->urlGenerator->generate(self::LOGIN_ROUTE);
    }
}

If you need any other part of my project please tell me, Thanks !



Solution 1:[1]

It's probably a cache problem, you have 2 different solutions, remove var/cache/prod folder and run composer install

But the clear method is to clear cache with the command that are made for it php bin/console cache:clear

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TomLorenzi