Symfony login not working

I use symfony 2.8, I am new to symfony, I have implemented login and registration, registration works fine, but when I log in, it shows this error

Type error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, string given, called in C:\xampp\htdocs\blog\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php on line 96 

Now I'm a little confused in the implementation of ROLES, I have a user table in the database,

TABLE OF USERS

 id Primary int(11) name varchar(255) email Index varchar(255) password varchar(64) roles varchar(255) created_at datetime 

Custom

 public function setRoles($roles) { $this->roles = $roles; } public function getRoles() { return $this->roles; } 

security.yml firewall section

 firewalls: # disables authentication for assets and the profiler, adapt it according to your needs dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: anonymous: ~ form_login: login_path: login check_path: login pattern: ^/ http_basic: ~ provider: our_db_provider # activate different ways to authenticate # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate #http_basic: ~ # https://symfony.com/doc/current/security/form_login_setup.html #form_login: ~ logout: path: /logout target: / 

If I change the getRoles function to return an array like this

 public function getRoles() { return array('ROLE_USER'); } 

In this case, an error is displayed on the registration page.

 The value of type "array" cannot be converted to a valid array key. 
0
source share
1 answer

It looks like you are passing a string for your role. Pass it as an array!

 public function __construct($user, $credentials, $providerKey, array $roles = array()) 

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php#L34

So, if the role was Admin , try passing array('admin') .

0
source

Source: https://habr.com/ru/post/970225/


All Articles