Skip to content

Commit fec0037

Browse files
committed
Create login page and implement login of user
1 parent e80b948 commit fec0037

File tree

4 files changed

+82
-16
lines changed

4 files changed

+82
-16
lines changed

controllers/SiteController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use app\core\Application;
1212
use app\core\Controller;
1313
use app\core\Request;
14+
use app\models\LoginForm;
1415
use app\models\User;
1516

1617
/**
@@ -28,10 +29,20 @@ public function home()
2829
]);
2930
}
3031

31-
public function login()
32+
public function login(Request $request)
3233
{
34+
$loginForm = new LoginForm();
35+
if ($request->getMethod() === 'post') {
36+
$loginForm->loadData($request->getBody());
37+
if ($loginForm->validate() && $loginForm->login()) {
38+
Application::$app->response->redirect('/');
39+
return;
40+
}
41+
}
3342
$this->setLayout('auth');
34-
return $this->render('login');
43+
return $this->render('login', [
44+
'model' => $loginForm
45+
]);
3546
}
3647

3748
public function register(Request $request)

models/LoginForm.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* User: TheCodeholic
4+
* Date: 7/25/2020
5+
* Time: 9:36 AM
6+
*/
7+
8+
namespace app\models;
9+
10+
11+
use app\core\Model;
12+
13+
/**
14+
* Class LoginForm
15+
*
16+
* @author Zura Sekhniashvili <[email protected]>
17+
* @package app\models
18+
*/
19+
class LoginForm extends Model
20+
{
21+
public string $email = '';
22+
public string $password = '';
23+
24+
public function rules()
25+
{
26+
return [
27+
'email' => [self::RULE_REQUIRED],
28+
'password' => [self::RULE_REQUIRED],
29+
];
30+
}
31+
32+
public function labels()
33+
{
34+
return [
35+
'email' => 'Your Email address',
36+
'password' => 'Password'
37+
];
38+
}
39+
40+
public function login()
41+
{
42+
$user = User::findOne(['email' => $this->email]);
43+
if (!$user) {
44+
$this->addError('email', 'User does not exist with this email address');
45+
return false;
46+
}
47+
if (!password_verify($this->password, $user->password)) {
48+
$this->addError('password', 'Password is incorrect');
49+
return false;
50+
}
51+
return true;
52+
}
53+
}

public/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
$app->router->get('/register', [SiteController::class, 'register']);
2828
$app->router->post('/register', [SiteController::class, 'register']);
2929
$app->router->get('/login', [SiteController::class, 'login']);
30+
$app->router->post('/login', [SiteController::class, 'login']);
3031
$app->router->get('/contact', [SiteController::class, 'contact']);
3132
$app->router->get('/about', [AboutController::class, 'index']);
3233

views/login.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
<h1>Contact page</h1>
2-
3-
<form>
4-
<div class="form-group">
5-
<label>Email address</label>
6-
<input type="email" class="form-control" name="email">
7-
<small class="form-text text-muted">We'll never share your email with anyone else.</small>
8-
</div>
9-
<div class="form-group">
10-
<label>Password</label>
11-
<input type="password" class="form-control" name="password">
12-
</div>
13-
<button type="submit" class="btn btn-primary">Submit</button>
14-
</form>
1+
<?php
2+
3+
/** @var $model \app\models\LoginForm */
4+
5+
use app\core\form\Form;
6+
7+
?>
8+
9+
<h1>Login</h1>
10+
11+
<?php $form = Form::begin('', 'post') ?>
12+
<?php echo $form->field($model, 'email') ?>
13+
<?php echo $form->field($model, 'password')->passwordField() ?>
14+
<button class="btn btn-success">Submit</button>
15+
<?php Form::end() ?>

0 commit comments

Comments
 (0)