getUser->loggin($name, $password) probehne, ale uzivatel neni prihlasen
- warriotox
- Člen | 30
Prosim Vas, jako vzdy jsem nahodil nejnovejsi nette a zacinam novy projekt, momentalne jsem pridal login, ale neco je tu rozbite. Do formulare napisi jmeno a heslo uzivatele ktery je v databazi a dam odeslat. jsem presmerovan na renderDefault v homepage presenteru, ale mam tu
<?php echo $user->isLoggedIn() ? 'ano' : 'ne'; ?>
a hned tu je tady napsano „ne“ a jakmile reloadnu stranku, nebo zavolam na nejakou funkci uvnitr homepage presenteru, tak se spusti ten before render a presmeruje me to na sign:in, protoze ten login neprobehl spravne. Vubec netusim co mi unika, ten kod je totozny s kodem co mam v posledni aplikaci a tam mi ten login bezi zcela bez problemu. Problem ani neni v tom, ze by to pustilo cokoliv skrz prihlaseni, kdyz napisu spatne jmeno nebo heslo, tak me to dal nepusti, tak nechapu kdyz to spravne overi ze uzivatel existuje v DB ze pak spravne neprobehne to „->login“
Nize Homepage a Sign presenter. Beru jakoukoliv radu nebo tip. Dekuji
homepage presenter
use Nette;
use App\Model;
use Nette\Application\UI;
use Nette\Application\UI\Form;
use Nette\Utils\Strings;
use Nette\DateTime;
use Nette\Security\Passwords;
use Nette\Security\User;
public function beforeRender(){
if (!$this->getUser()->isLoggedIn()){
$this->redirect('Sign:in');
}
}
signin presenter
<?php
namespace App\Presenters;
use Nette;
use App\Forms\SignFormFactory;
use Nette\Security\User;
class SignPresenter extends BasePresenter
{
/** @var SignFormFactory @inject */
public $factory;
/**
* Sign-in form factory.
* @return Nette\Application\UI\Form
*/
protected function createComponentSignInForm()
{
$form = $this->factory->create();
$form->onSuccess[] = $this->signInFormSucceeded;
return $form;
}
public function signInFormSucceeded($form) {
$values = $form->getValues();
if ($values->remember) {
$this->getUser()->setExpiration('14 days', FALSE);
} else {
$this->getUser()->setExpiration('20 minutes', TRUE);
}
try {
$this->getUser()->login($values->username, $values->password);
$this->redirect('Homepage:');
} catch (Nette\Security\AuthenticationException $e) {
$form->addError($e->getMessage());
}
}
public function actionOut()
{
$this->getUser()->logout();
$this->flashMessage('You have been signed out.');
$this->redirect('in');
}
}
Editoval warriotox (15. 12. 2015 22:27)
- Anaxim
- Člen | 3
Ja som mal podobny problem. Po dlhych hodinach som prisiel na to ze mam problem z Hashovanim hesla kedze defaultne v UserManager.php je to nastavene. Ked som hashovanie vypol tak mi to fici. Teraz len musim prist na to ako to sprovoznit i z hashovanim :) Snad ti to pomoze odhalit pricinu
function authenticate(array $credentials)
{
list($username, $password) = $credentials;
$row = $this->database->table('users')
->where('username', $username)->fetch();
if (!$row) {
throw new NS\AuthenticationException('User not found.');
}
if ($password !== $row->password) {
throw new NS\AuthenticationException('Invalid password.');
}
return new NS\Identity($row->id, $row->role, array('username' => $row->username));
}