Symfony Login Form TODO: provide a valid redirect inside C:\xampp\htdocs\LicentaTenis\src\Security\AppCustomAuthenticator.php

Viewed 4196

I am new to symfony and this is my first project and I'm trying to create a login form. I created it using make:auth, but when I try to login I get this error: provide a valid redirect inside error

I replaced

throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);

with

return new RedirectResponse($this->urlGenerator->generate('home'));

but it shows me the same error.

This is my home route:

/**
* @Route("/", name="home")
* @Method({"GET"})
*/
public function index(){
    return $this->render('tenis/index.html.twig');
}
3 Answers

For the first solution, you can just make a null return in the onAuthenticationSuccess(...) function (line 99 of your AppCustomAthenticator.php) if you don't have any particular processing to do when the User has connected with success and add the redirection route directly in the ./config/packages/security.yaml file like this.

security:
providers:
    # used to reload user from session & other features (e.g.switch_user) #
    app_user_provider:
        entity:
            class: App\Entity\User
            property: email

encoders:
    # use your user class name here
    App\Entity\User:
        # Use native password encoder
        # This value auto-selects the best possible hashing algorithm
        # (i.e. Sodium when available).
        algorithm: auto

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: lazy
        form_login:
            login_path: app_login
            check_path: app_login
            csrf_token_generator: security.csrf.token_manager # It's optional
            default_target_path: home # add here your redirect path
            always_use_default_target_path: true
        guard:                                        # you need to check this file and 
            authenticators:                           # make sure that all your configurations 
                - App\Security\LoginFormAuthenticator # are right
        logout:
            path: app_logout
            target: app_login

Or you can comment out the line 99 of your AppCustomAthenticator.php (in the onAuthenticationSuccess(...) function)

  throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);

and change it to.

return new RedirectResponse($this->urlGenerator->generate('home'));

You can change the value of 'home' by the name of your redirect symfony route.

Make sure you save the change. If it does not work, try to clear the cache:

bin/console cache:clear

In your SecurityAuthenticator file, find the function SecurityAuthenticator and uncomment this line: return new RedirectResponse ($ this-> urlGenerator-> generate ('home')); to put your redirection

Related