1

config/packages/security.yaml:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt

      # ...

      providers:
        our_db_provider:
          entity:
            class: App\Entity\User
            property: username
            # if you're using multiple entity managers
            # manager_name: customer

            firewalls:
              main:
                anonymous: ~
                form_login:
                  login_path: login
                  check_path: login

                  # ...
                  access_control:
                    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
                    - { path: ^/, roles: ROLE_ADMIN }

src/Controller/SecurityController.php:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends Controller
{
  /**
  * @Route("/login", name="login")
  */
  public function login(Request $request, AuthenticationUtils $authenticationUtils)
  {
      // 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', array(
          'last_username' => $lastUsername,
          'error'         => $error,
      ));
  }
}

src/Controller/DefaultController.php

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends Controller
{
    /**
     * @Route("/admin")
     */
    public function admin()
    {
        return new Response('<html><body>Admin page!</body></html>');
    }
}

templates/security/login.html.twig:

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

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path('login') }}" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />

    {#
        If you want to control the URL the user
        is redirected to on success (more details below)
        <input type="hidden" name="_target_path" value="/account" />
    #}

    <button type="submit">login</button>
</form>

I want to load my /admin or /login page I get the error:

InvalidConfigurationException Unrecognized option "providers" under "security.encoders.App\Entity\User"

at ArrayNode->normalizeValue(array('providers' => array('our_db_provider' => array('entity' => array('class' => 'App\Entity\User', 'property' => 'email', 'firewalls' => array('main' => array('anonymous' => null, 'form_login' => array('login_path' => 'login', 'check_path' => 'login', 'access_control' => array(array('path' => '^/login', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'), array('path' => '^/', 'roles' => 'ROLE_ADMIN'))))))))))in BaseNode.php line 368

1 Answer 1

2

Your tabulation is wrong in your security.yaml configuration. You have this:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt

      # ...

      providers:

It should instead be:

security:
  encoders:
    App\Entity\User:
      algorithm: bcrypt

      # ...

  providers:

The encoders and providers should both 1st-level children under the security configuration.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you, this was solving most errors. But there is still this error: Cannot determine controller argument for "App\Controller\SecurityController::login()": the $request argument is type-hinted with the non-existent class or interface: "App\Controller\Request". Did you forget to add a use statement?
Add use Symfony\Component\HttpFoundation\Response; to the top of your SecurityController class
Thank you for the advise. I did it, but still the same error
Oops I meant use Symfony\Component\HttpFoundation\Request;
Yes, I am working the documentation right now through, I created this login by the documentation, but I am just beginning, so still have some problems :) But thank you very much for your help!! Really :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.