Symfony 4 guard not authenticate user

1.3k views Asked by At

I'm using guard as my authentication layer for my symfony 4 flex app.

whenever I enter in my username and password it automatically redirects me to the login page, no errors just redirects me. In my logs it shows that I couldn't log in but the form isn't showing any of that

security.yaml

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt
            cost: 12
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: ~ }
        chain_provider:
            chain:
                providers: [db_provider_username, db_provider_email]
        db_provider_username:
            entity:
                class: App\Entity\User
                property: username
        db_provider_email:
            entity:
                class: App\Entity\User
                property: email
        oauth:
            id: knpu.oauth2.user_provider

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true

        secured_area:
            anonymous: ~
            provider: chain_provider
            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
                    - App\Security\GoogleAuthenticator
                entry_point: App\Security\LoginFormAuthenticator
            remember_me:
                secret:   '%kernel.secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /
            logout:
                path: app_logout
            logout_on_user_change: true



            # activate different ways to authenticate

            # http_basic: true
            # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: true
            # https://symfony.com/doc/current/security/form_login_setup.html

    # 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: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        # - { path: ^/profile, roles: ROLE_USER }

LoginFormAthenticator.php

<?php

namespace App\Security;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    private $entityManager;
    private $urlGenerator;
    private $csrfTokenManager;
    private $passwordEncoder;

    public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->entityManager = $entityManager;
        $this->urlGenerator = $urlGenerator;
        $this->csrfTokenManager = $csrfTokenManager;
        $this->passwordEncoder = $passwordEncoder;
    }

    public function supports(Request $request)
    {
        $request->request->has("username") && $request->request->has("password");
        //return 'app_login' === $request->attributes->get('_route') && $request->isMethod('POST');
    }

    public function getCredentials(Request $request)
    {
        $credentials = [
            'username' => $request->request->get('username'),
            'password' => $request->request->get('password'),
            'csrf_token' => $request->request->get('_csrf_token'),
        ];
        $request->getSession()->set(
            Security::LAST_USERNAME,
            $credentials['username']
        );

        return $credentials;
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $token = new CsrfToken('authenticate', $credentials['csrf_token']);
        if (!$this->csrfTokenManager->isTokenValid($token)) {
            throw new InvalidCsrfTokenException();
        }

        $user = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $credentials['username']]);

        if (!$user) {
            // fail authentication with a custom error
            throw new CustomUserMessageAuthenticationException('Username could not be found.');
        }

        return $user;
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
    }

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

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

    protected function getLoginUrl()
    {
        return $this->urlGenerator->generate('app_login');
    }
}

SecurityController.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;

class SecurityController extends AbstractController
{
    /**
     * @Route("/login", name="app_login")
     */
    public function login(AuthenticationUtils $authenticationUtils, TranslatorInterface $translator): Response
    {
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'types' => 'login', 'message' => $translator->trans('All Categories')]);
    }
}

login.html.twig

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

{% block title %}NALO Stream : {{ 'Login Page'|trans }}{% endblock %}

{% block body %}
<div class="row">
    <div class="col s5" style="margin: 60px auto; float: none !important;">
        <div class="card">
           <div class="card-content white-text">
               <p class="card-title blue lighten-3 card-panel">{{ 'Sign In'|trans }}</p>
               {% if error %}
                   <div class="card-panel red lighten-3" style="margin-top: 20px;text-align: center">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
               {% endif %}

               <form method="post" style="text-align: center" action="{{ path('app_login') }}">
                   <div class="input-field">
                        <input type="text" name="username" id="username" value="{{ last_username }}" autofocus required/>
                        <label for="username">{{ 'Username'| trans }}</label>
                   </div>

                   <div class="input-field">
                       <input type="password" name="password" id="password" required/>
                       <label for="password">{{ 'Password'| trans }}</label>
                   </div>

                   <div class="row" style="margin-top:30px;text-align: center">
                       <div class="col s6">
                           <label>
                               <input type="checkbox" class="filled-in" checked="checked" name="_remember_me" id="remember-me"/>
                               <span>{{ 'Remember Me'|trans }}</span>
                           </label>
                       </div>
                       <div class="col s6" style="text-align: center">
                           <a href="{{ path('app_user_forget_password') }}" >{{ 'Forget Password?'|trans }}</a>
                       </div>
                   </div>
                   <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">

                   <button class="waves-block waves-effect waves-light btn" type="submit" style="width:100%">
                       <i class="material-icons left">https</i> {{ 'Sign In'|trans }}
                   </button>

                   <p style="margin-top: 30px">Not a member? <a href="{{ path('app_register') }}">{{ 'Sign up'|trans }}</a></p>
                   <br>
                   <p>Sign In With:</p>
                   <br>
                   <a href="{{ path('app_connect_google') }}" class="btn-floating btn-large waves-effect waves-light btn-facebook blue accent-3 white-text"><i class="fab fa-google-plus"></i></a>
               </form>
           </div>
        </div>
    </div>
</div>
{% endblock %}

2

There are 2 answers

0
Arleigh Hix On

Your LoginFormAthenticator::supports method should return true if the authenticator is to handle the request.
You have:

public function supports(Request $request)
{
    $request->request->has("username") && $request->request->has("password");
    //return 'app_login' === $request->attributes->get('_route') && $request->isMethod('POST');
}

That does not return anything, so the authenticator never runs.
Maybe you meant to do something like the following?

public function supports(Request $request)
{
    return ($request->isMethod('POST') && $request->request->has("username") && $request->request->has("password"));
}
0
cybernet2u On

add this before form rendering in login.html.twig

            {% if error %}
                <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
            {% endif %}