0
votes

I'm using symfony 2.8, I'm new to symfony,I have implemented login and registration, registration is working fine but when I login it showing 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 bit confused in ROLES implementation, I have users table in DB,

USERS TABLE

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

User Entity

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

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

Security.yml firewalls 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 my getRoles function to return array like this

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

In this case it is showing error on registration page.

The value of type "array" cannot be converted to a valid array key.
1
why downvoted. ? - Aamir
Don’t mind down votes, you need to get used to it in stackoverflow, a lot of mad people out there, and this makes new comers a little bit offended :/ but what can we possibly do about it! I’ll upvote so it’s back to 0 lol - teeyo
I need to check thoroughly again on implementing roles in symfony. i'm new to it so I'm still on the way. - Aamir

1 Answers

0
votes

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').