Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirecting not working with custom listener #1222

Closed
danielpeci opened this issue May 8, 2024 · 0 comments
Closed

Redirecting not working with custom listener #1222

danielpeci opened this issue May 8, 2024 · 0 comments

Comments

@danielpeci
Copy link

I am using the Lexik Bundle to secure my API (API Platform), as well as to authenticate users via login (Symfony UX React). However, the redirection on custom authentication is not working. I receive a 200 status code and the "view" in response looks good, but nothing happens (it stays on the login page). Any ideas? Or is there a better way to implement the logic?

AuthenticationSuccessListener.php

class AuthenticationSuccessListener extends AuthenticationSuccessHandler
{
    private RouterInterface $router;

    public function __construct(
        RouterInterface $router
    ) {
        $this->router = $router;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token): Response
    {
        return new RedirectResponse($this->router->generate('app_management'));
    }
}

security.yml

security:
    # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
    # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email

    firewalls:
        login:
            pattern: ^/api/login
            stateless: true
            json_login:
                check_path: /api/login_check
                username_path: email
                success_handler: app.listener.authentication_success_response
                failure_handler: lexik_jwt_authentication.handler.authentication_failure


        api:
            pattern: ^/api/
            stateless: true
            jwt: ~


            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#the-firewall

            # 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: ^/api/login, roles: PUBLIC_ACCESS }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

services.yml

    app.listener.authentication_success_response:
            class: App\Listeners\AuthenticationSuccessListener
            tags:
                - { name: kernel.event_listener, event: lexik_jwt_authentication.handler.authentication_success, method: onAuthenticationSuccess }

Login.jsx

const Login = () => {
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');

    const handleEmailChange = (e) => setEmail(e.target.value);
    const handlePasswordChange = (e) => setPassword(e.target.value);

    const handleSubmit = (event) => {
        event.preventDefault();
        axios.post(
            'https://localhostindex.php/api/login_check',
            {email: email, password: password},
            {
                headers: {
                    'Content-Type': 'application/ld+json'
                }
            }
        )
            .then(() => {
                console.log('good');
            })
            .catch(() => {
                console.log('bad');
            });
    };
    <form method="post" onSubmit={handleSubmit}>
        <Input
            type="email"
            variant="bordered"
            size="lg"
            label="Email"
            placeholder="Enter your email"
            labelPlacement="outside"
            className="pt-5 pb-5"
            value={email}
            id="email"
            name="email"
            onChange={handleEmailChange}
        />
        <Input
            type="password"
            variant="bordered"
            size="lg"
            label="Password"
            placeholder="Enter your password"
            labelPlacement="outside"
            className="pt-5 pb-12"
            value={password}
            id="password"
            name="password"
            onChange={handlePasswordChange}
        />
        <button type="submit" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
            Login
        </button>
    </form>

Controller (Where I want to redirect)

    #[Route('/management', name: 'app_management')]
    public function index(): Response
    {
        return $this->render('management/index.html.twig', [
            'controller_name' => 'ManagementController',
        ]);
    }

View

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

{% block body %}
    <div {{ react_component('Management/Management')}}></div>
{% endblock %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant