2
votes

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

1
I think you're 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.JimL
@JimL my Class Users which is Entity already implements UserInterface : class Users implements UserInterfacePHPer
Great! But as you see from the docs I link to UserPasswordEncoder::isPasswordValid expects two arguments, UserInterface $user and string $plainPassword. Not the $hash, $plainPassword you're sending itJimL
just send isPasswordValid($user, $password) and you are done, @JimL is right you are looking at interface instead of implementationkunicmarko20
That's true, so how I can create this UserInterface $user object?PHPer

1 Answers

3
votes

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)) {