Catchable Fatal Error: argument 4 passed to UsernamePasswordToken :: __ construct () must be an array, null

When I log into my Symfony application (with the correct username and password), the following error appears:

ContextErrorException: Catchable Fatal Error: argument 4 passed to Symfony \ Component \ Security \ Core \ Authentication \ Token \ UsernamePasswordToken :: __ construct () must be an array with a null value, called in D: \ xampp \ htdocs \ essweb \ provider \ symfony \ symfony \ src \ Symfony \ Component \ Security \ Core \ Authentication \ Provider \ UserAuthenticationProvider.php on line 96 and defined in D: \ xampp \ htdocs \ essweb \ vendor \ symfony \ symfony \ src \ Symfony \ Component \ Security \ Core \ Authentication \ Token \ UsernamePasswordToken.php line 36

security.yml

firewalls: admin_area: pattern: ^/ anonymous: ~ form_login: login_path: / check_path: /login_check default_target_path: /user failure_path: / #username_parameter: username #password_parameter: password remember_me: true remember_me: key: "%secret%" lifetime: 31536000 path: / domain: ~ always_remember_me: true logout: path: /logout target: / 

Updated:

Login form:

 <form class="form-signin form-group" action="{{ path('login_check') }}" method="post"> Username: <input type="text" class="form-control" name="_username" placeholder="Username" value="{{ last_username }}" required="required"> Password: <input type="text" class="form-control" name="_password" placeholder="" value="{{ last_username }}" required="required"> <button class="btn btn-sm btn-primary btn-block" type="submit">Sign in</button> 
+9
source share
3 answers

Basically this error message:

The 4th argument to UsernamePasswordToken::__construct() must be an array, but it is null . It was called in UserAuthenticationProvider on line 96 .

If you look at this code, you will see that the 4th argument to UsernamePasswordToken::__construct() is $roles . So it should be an array, but instead it gets null .

I assume that you wrote your own user object and that the getRoles() method of your user object returns null instead of an array of roles. So just change this method to something like this:

 /** * Returns the roles granted to the user. * * @return Role[] The user roles */ public function getRoles() { return array('ROLE_USER'); } 

Of course, the actual code may be different (you may want to keep the user roles in the database) while getRoles() returns an array of strings or a Role .

+31
source

I fix this error by returning roles:

 public function getRoles() { return $this->roles->toArray(); } 
+4
source

This error is caught because the current value of the role property has a value of zero, however you must give it an array of roles, so to solve it, simply return the role property:

 public function getRoles() { return $this->roles; } 
0
source

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


All Articles