treyBake
treyBake

Reputation: 6560

Redirect after form success symfony 4

I followed this tutorial: https://symfony.com/doc/current/security/form_login_setup.html

to build my form and it renders ok, I have a user created in my table and use its username and password to log in but get redirected to a blank page, but I've set it up to go to /dashboard

my security.yaml

encoders:
    App\Entity\User: bcrypt
providers:
    in_memory: { memory: ~ }
    user_provider:
        entity:
            class: App\Entity\User
            property: username
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: ~
        http_basic: ~
        provider:   user_provider
        form_login:
            login_path: login
            check_path: login
            default_target_path: /dashboard

I even tried using the hidden _target input:

<input type="hidden" name="_target_path" value="{{ path('dashboard') }}" />

LoginController.php

<?php
    namespace App\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

    class LoginController extends Controller
    {
        public function login(Request $request, AuthenticationUtils $authenticationUtils)
        {
            $error    = $authenticationUtils->getLastAuthenticationError();
            $lastUser = $authenticationUtils->getLastUsername();

            return $this->render('login/form.html.twig', array('last_username' => $lastUser, 'error' => $error));
        }
    }

full code base: https://github.com/BPSBiro/symfony-forms-errors

but it just redirects to /login as a blank page. I know it's not erroring (credentials wise) because if I type gibberish into both fields then it goes back to /login with the form and error message (invalid credentials).

How do I redirect after form success?

Upvotes: 2

Views: 1329

Answers (2)

Cerad
Cerad

Reputation: 48865

So I brought down your github and ran it unchanged. Created a user and then tried to login. Got an exception pointing to App\Entity\User::serialize. You had a minor problem here:

public function serialize()
{
    # causes infinite nesting
    # return $this->serialize(array($this->id, $this->username, $this->password));

    # the fix
    return serialize(array($this->id, $this->username, $this->password));
}

The mystery is why you got a blank page instead of an exception. I used the built in server for testing:

bin/console server:start

I'm guessing your Apache configuration is actually running in production mode which is suppressing your errors.

Upvotes: 3

fgamess
fgamess

Reputation: 1324

Why do you keep http_basic: ~ if you are using traditional form login ? Please remove this and try again

Upvotes: 0

Related Questions