metoda autheticate nieje compatibilna s Interfacom

Jozo323
Člen | 3
+
0
-

Dobrý deň , začínam s nette a s touto chybou sa hrám už dobrú chvílu :

chybova hlaska z tracy:

Declaration of App\CoreModule\Model\MyAuthenticator::authenticate(array $credentials): App\CoreModule\Model\Nette\Security\Identity must be compatible with Nette\Security\IAuthenticator::authenticate(array $credentials): Nette\Security\IIdentity

a toto je MyAutheticator :

<?php

namespace App\CoreModule\Model;

use Nette\Security as NS;

class MyAuthenticator implements NS\IAuthenticator
{
    private $database;

    private $passwords;

    public function __construct(Nette\Database\Context $database, Nette\Security\Passwords $passwords)
    {
        $this->database = $database;
        $this->passwords = $passwords;
    }

    public function authenticate(array $credentials): Nette\Security\Identity
    {
        [$username, $password] = $credentials;

        $row = $this->database->table('users')
            ->where('username', $username)->fetch();

        if (!$row) {
            throw new Nette\Security\AuthenticationException('User not found.');
        }

        if (!$this->passwords->verify($password, $row->password)) {
            throw new Nette\Security\AuthenticationException('Invalid password.');
        }

        return new Nette\Security\Identity($row->id, $row->role, ['username' => $row->username]);
    }
} ?>
ali
Člen | 342
+
-1
-
public function authenticate(array $credentials): NS\Identity
MajklNajt
Člen | 470
+
+2
-

@Jozo323 Ahoj, importujteš Nette\Security ako NS, čiže všade v kóde namiesto Nette\Security používaj ten definovaný alias NS.

Alebo druhá možnosť – nerob aliasy, naimportuj si iba Nette a všade používaj plný namespace:

use Nette;

Prípadne tretia možnosť – naimportuj si všetky používané triedy a potom v kóde používaj iba ich názvy bez namespace:

use Nette\Database\Context;
use Nette\Security\IAuthenticator;
use Nette\Security\Passwords;
use Nette\Security\Identity;
use Nette\Security\AuthenticationException;
Jozo323
Člen | 3
+
0
-

Dakujem Problem Solved

Tomac1
Člen | 3
+
0
-

Pěkný den,

obávám se, že tam je skutečně problém s kompatibilitou interfacu. Dokážete někdo poradit?
Authenticator mám v app/Authenticator.php.

Chyba:
Compile Error
Declaration of Authenticator::authenticate(array $credentials): Nette\Security\Identity must be compatible with Nette\Security\IAuthenticator::authenticate(array $credentials): Nette\Security\IIdentity

<?php
declare(strict_types=1);

use Nette\Security as NS;

class Authenticator implements NS\IAuthenticator
{
	private $database;

	private $passwords;

	public function __construct(Nette\Database\Context $database, NS\Passwords $passwords)
	{
		$this->database = $database;
		$this->passwords = $passwords;
	}

	public function authenticate(array $credentials): NS\Identity
	{
		[$username, $password] = $credentials;

		$row = $this->database->table('users')
			->where('username', $username)->fetch();

		if (!$row) {
			throw new NS\AuthenticationException('User not found.');
		}

		if (!$this->passwords->verify($password, $row->password)) {
			throw new NS\AuthenticationException('Invalid password.');
		}

		return new NS\Identity($row->id, $row->role, ['username' => $row->username]);
	}
}

Editoval Tomac1 (21. 7. 2019 14:54)

ali
Člen | 342
+
0
-

Poradne si precti tu chybovou hlasku.

Nette\Security\Identity
vs
Nette\Security\IIdentity

Tomac1
Člen | 3
+
0
-

ali napsal(a):

Poradne si precti tu chybovou hlasku.

Nette\Security\Identity
vs
Nette\Security\IIdentity

Ahoj ali. Čtu jí pořádně. IIdentity je přeci existující interface. Pokud tam nemá být, tak je chyba v Nette 3 a interfacu IAuthenticator, který IIdentity používá.

Ale do jádra nehrabu, takže v čem je tedy chyba?

Ten můj kód je v podstatě převzatý z příkladu: https://doc.nette.org/…thentication

Editoval Tomac1 (21. 7. 2019 15:38)

nightfish
Člen | 468
+
0
-

Tomac1 napsal(a):
Ale do jádra nehrabu, takže v čem je tedy chyba?

@Tomac1 Chyba je na řádku

public function authenticate(array $credentials): NS\Identity

Správně to má být

public function authenticate(array $credentials): NS\IIdentity

(Protože v PHP jsou – zatím – návratové typy funkcí invariantní, musíš při implementaci metody rozhraní nebo jejím přepisu v potomkovi dodržet naprosto stejnou signaturu.)

Mysteria
Člen | 797
+
0
-

Chyba je v tom, že metoda authenticate `musí vracet `IIdentity `a ty vracíš `Identity. Takže přepiš NS\Identity na NS\IIdentity a problém vyřešen. To je přesně to, co ti říká i ta chybová hláška.

Tomac1
Člen | 3
+
+3
-

nightfish napsal(a):

Tomac1 napsal(a):
Ale do jádra nehrabu, takže v čem je tedy chyba?

@Tomac1 Chyba je na řádku

public function authenticate(array $credentials): NS\Identity

Správně to má být

public function authenticate(array $credentials): NS\IIdentity

(Protože v PHP jsou – zatím – návratové typy funkcí invariantní, musíš při implementaci metody rozhraní nebo jejím přepisu v potomkovi dodržet naprosto stejnou signaturu.)

Ano děkuji. Zkusil jsem to sám na základě komentu aliho a pomohlo to. Čili chyba je v příkladu na webu. Chybu jsem opravil přes github.

Pomohla tedy změna na public function authenticate(array $credentials): NS\IIdentity