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

    $user = $token->getUser();
    
    if (!$user instanceof User) {
        return new RedirectResponse($this->urlGenerator->generate('app_error'));
    }

    
    if (in_array('ROLE_ADMIN', $user->getRoles())) {
        return new RedirectResponse($this->urlGenerator->generate('app_admin'));
    } else {
        return new RedirectResponse($this->urlGenerator->generate('app_user'));
    }
}

I was expecting that after logging in I would be rediretcted to the admin or user page depending on the role. But it keeps bringing me to the homepage and give errors.

What am I doing wrong?

philipxy
  • 14,867
  • 6
  • 39
  • 83
  • Make sure to run these codes in the CLI before starting a new symfony project: Composer install composer require annotations composer require --dev symfony/maker-bundle composer require symfony/form composer require doctrine php bin/console doctrine:database:create php bin/console make:controller HomeController AdminController UserController php bin/console make:user php bin/console make:registration-form php bin/console make:auth php bin/console make:migration – NoobDeveloper69 May 06 '23 at 12:07
  • https://getbootstrap.com/docs/5.0/getting-started/introduction/ https://getbootstrap.com/docs/5.0/components/navbar/ – NoobDeveloper69 May 06 '23 at 12:18
  • DATABASE_URL="mysql://root:@127.0.0.1:3306/donkeytravel?serverVersion=10.4.27-MariaDB&charset=utf8mb4" – NoobDeveloper69 May 06 '23 at 19:03
  • php bin/console make:entity – NoobDeveloper69 May 06 '23 at 19:19
  • Inside the registrationcontroller: $user = new User(); $role = ["ROLE_USER"]; $user -> setRoles($role); – NoobDeveloper69 May 07 '23 at 11:58
  • [mre] Please clarify via edits, not comments. Please delete & flag obsolete comments. Please use standard spelling & punctuation. – philipxy May 09 '23 at 00:47
  • "give errors" - can you share any of these error messages, along with the code involved? – Nico Haase May 09 '23 at 05:12

1 Answers1

1

This code is not checking if $user is an instance of the expected User class. This can cause an error if $token->getUser() returns something other than a User instance that doesn't have the getRoles() method, and results in a fatal error.

Here is how you can fix this mistake:

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

        // Get the authenticated user
        $user = $token->getUser();

        // Check the user's role
        if (in_array('ROLE_ADMIN', $user->getRoles())) {
            // Redirect to the app_admin page
            return new RedirectResponse($this->urlGenerator->generate('app_admin'));
        } else {
            // Redirect to the app_user page
            return new RedirectResponse($this->urlGenerator->generate('app_user'));
        }
    }