PHPer
PHPer

Reputation: 39

Encoder->IsPasswordValid must implement interface \UserInterface

I am trying to validate user, I can't user FOSub bcz it's not compatible with Symfony 4, I am coding something mine. I pass username and password through ajax to service. Method which trying to login user :

public function login($username, $password) {

$user = $this->entityManager->getRepository("App:Users")
    ->findOneBy(array('username' => $username));
}

if(!$this->encoder->isPasswordValid($user->getPassword(), $password)) {
    return new Response(
        'Username or Password not valid.',
        Response::HTTP_UNAUTHORIZED,
        array('Content-type' => 'application/json')
    );
}

}

When I call this method I have error :

Type error: Argument 1 passed to Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::isPasswordValid() must implement interface Symfony\Component\Security\Core\User\UserInterface, string given, called in /src/Service/Login.php on line 60

Why it's telling me that I have to implement some service when in Symfony documentation there is

isPasswordValid(string $encoded, string $raw, string $salt), Checks a raw password against an encoded password.

I don't understand this...

Upvotes: 2

Views: 3966

Answers (1)

JimL
JimL

Reputation: 2541

You're simply looking at the wrong docs, PasswordEncoderInterface has the definition you mention, but you should be looking at UserPasswordEncoder.

Your User class needs to implement UserInterface.

UserPasswordEncoder::isPasswordValid expects two arguments, UserInterface $user, string $plainPassword. Not the $hash, $plainPassword you're sending it

if(!$this->encoder->isPasswordValid($user, $password)) {

Upvotes: 4

Related Questions