Skip to content

Commit 9045008

Browse files
committed
login modal created
1 parent 325741b commit 9045008

File tree

20 files changed

+198
-17
lines changed

20 files changed

+198
-17
lines changed

.db/dummy.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ INSERT INTO event (title) VALUES ('Na cepu nove Plzen 10');
66
INSERT INTO event (title, event_time, event_place) VALUES ('Patecni grilovane kure', '2023-10-26 10:38:10', 'Snyt - Vinohrady');
77
INSERT INTO event (title) VALUES ('Zahradka zavrena');
88
INSERT INTO event (title) VALUES ('Hledame kuchare');
9+
10+
INSERT INTO user (email, password) VALUES ('test@gmail.com', 'test');

.db/schema.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ CREATE TABLE event (
77
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
88
PRIMARY KEY(id)
99
);
10+
11+
CREATE TABLE user (
12+
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
13+
email VARCHAR(255) NOT NULL,
14+
password VARCHAR(255) NOT NULL,
15+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
16+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
17+
PRIMARY KEY(id)
18+
);

app/Model/Repository/BaseRepository.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88

99
abstract class BaseRepository
1010
{
11-
public function __construct(private Connection $database)
12-
{
13-
}
14-
15-
public function getDibiConnection(): Connection
16-
{
17-
return $this->database;
11+
public function __construct(
12+
protected Connection $dibi
13+
) {
1814
}
1915
}

app/Model/Repository/EventRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EventRepository extends BaseRepository
1010

1111
public function findAll(): array
1212
{
13-
$result = $this->getDibiConnection()
13+
$result = $this->dibi
1414
->select('*')
1515
->from(self::TABLE);
1616

@@ -19,7 +19,7 @@ public function findAll(): array
1919

2020
public function getAllCount(): int
2121
{
22-
$result = $this->getDibiConnection()
22+
$result = $this->dibi
2323
->select('COUNT(id)')
2424
->from(self::TABLE);
2525

@@ -28,7 +28,7 @@ public function getAllCount(): int
2828

2929
public function findAllForPaginator(int $limit, int $offset): array
3030
{
31-
$result = $this->getDibiConnection()
31+
$result = $this->dibi
3232
->select('*')
3333
->from(self::TABLE)
3434
->limit($limit)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Repository;
6+
7+
use Dibi\Row;
8+
9+
class UserRepository extends BaseRepository
10+
{
11+
public function findOneBy(string $key, mixed $value): Row|null
12+
{
13+
$result = $this->dibi->query(
14+
"SELECT * FROM user WHERE {$key}=?",
15+
$value
16+
);
17+
18+
return $result->fetch();
19+
}
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Model\Service;
6+
7+
use App\Model\Repository\UserRepository;
8+
use Nette\Security\AuthenticationException;
9+
use Nette\Security\Authenticator;
10+
use Nette\Security\Passwords;
11+
use Nette\Security\SimpleIdentity;
12+
13+
class AuthenticationService implements Authenticator
14+
{
15+
public function __construct(
16+
protected Passwords $passwords,
17+
protected UserRepository $userRepository
18+
) {}
19+
20+
21+
/**
22+
* @throws AuthenticationException
23+
*/
24+
public function authenticate(string $user, string $password): SimpleIdentity
25+
{
26+
$user = $this->userRepository->findOneBy('email', $user);
27+
28+
if (!$user) {
29+
throw new AuthenticationException('Uzivatel nebyl nalezen');
30+
}
31+
if (!$this->passwords->verify($password, $user->password)) {
32+
throw new AuthenticationException('Nepsravne heslo');
33+
}
34+
35+
return new SimpleIdentity(
36+
$user->id,
37+
null,
38+
[
39+
'email' => $user->email
40+
]
41+
);
42+
}
43+
}

app/Model/Service/AuthorizationService.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace App\Model\Service;
46

7+
use Nette\Security\AuthenticationException;
58
use Nette\Security\User as NetteSecurityUser;
69

710
class AuthorizationService
@@ -15,4 +18,12 @@ public function isLoggedIn(): bool
1518
{
1619
return $this->netteSecurityUser->isLoggedIn();
1720
}
21+
22+
/**
23+
* @throws AuthenticationException
24+
*/
25+
public function login(string $email, string $password): void
26+
{
27+
$this->netteSecurityUser->login($email, $password);
28+
}
1829
}

app/Module/User/Register/RegisterPresenter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88

99
class RegisterPresenter extends BasePresenter
1010
{
11+
public function actionDefault(): void
12+
{
13+
14+
}
1115
}

app/Module/_components/Login/Login.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,38 @@
22

33
namespace App\Module\_components\Login;
44

5+
use App\Model\Service\AuthorizationService;
56
use App\Module\_components\BaseControl;
67
use Nette\Application\UI\Form;
8+
use Nette\Security\AuthenticationException;
79

810
class Login extends BaseControl
911
{
12+
public function __construct(
13+
protected AuthorizationService $authorizationService
14+
) {
15+
}
16+
1017
protected function createComponentForm(): Form
1118
{
1219
$form = new Form();
13-
$form->addText("email");
14-
$form->addPassword("password");
20+
$form->addEmail("email")->setRequired();
21+
$form->addPassword("password")->setRequired();
22+
$form->onSubmit[] = [$this, "handleLogin"];
1523

1624
return $form;
1725
}
1826

27+
public function handleLogin(Form $form): void
28+
{
29+
try {
30+
$values = $form->getValues();
31+
$this->authorizationService->login($values->email, $values->password);
32+
} catch (AuthenticationException) {
33+
$form->addError('Nesprávný E-mail nebo heslo');
34+
}
35+
}
36+
1937
public function render(): void
2038
{
2139
$this->template->setFile(__DIR__ . '/templates/login.latte');

app/Module/_components/Login/templates/login.latte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<div class="login shader-softest">
22
<div class="login__block">
3+
<svg class="login__icon-cross">
4+
<use xlink:href="{$basePath}/assets/image/icon/general.svg#icon-cross"></use>
5+
</svg>
36
<h1 class="login__heading">Login</h1>
47
<form n:name="form">
8+
<div class="login__message-box" n:foreach="$form->ownErrors as $error">
9+
<span class="login__message login__message--active login__message--error">{$error}</span>
10+
</div>
511
<ul>
612
<li class="login__form-item">
713
<div class="login__label-wrap">
@@ -23,7 +29,7 @@
2329
</li>
2430
</ul>
2531
<div class="login__links">
26-
<a n:href=":User:Register:" class="login__register">Register</a>
32+
<a href="{plink :User:Register:}" class="login__register">Register</a>
2733
<span class="login__forgot">Forgot password</span>
2834
</div>
2935
<div class="login__button-wrap">

0 commit comments

Comments
 (0)