Skip to content

Commit 0e1f378

Browse files
authored
Merge pull request #242 from jlopes90/develop
feat: add validation in LoginController
2 parents b0ccc14 + 4aea237 commit 0e1f378

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

src/Controllers/LoginController.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace CodeIgniter\Shield\Controllers;
44

55
use App\Controllers\BaseController;
6-
use CodeIgniter\HTTP\IncomingRequest;
76
use CodeIgniter\HTTP\RedirectResponse;
8-
use CodeIgniter\HTTP\Response;
97

108
class LoginController extends BaseController
119
{
@@ -27,18 +25,21 @@ public function loginView()
2725

2826
/**
2927
* Attempts to log the user in.
30-
*
31-
* @return Response|string
3228
*/
33-
public function loginAction()
29+
public function loginAction(): RedirectResponse
3430
{
35-
/** @var IncomingRequest $request */
36-
$request = service('request');
31+
// Validate here first, since some things,
32+
// like the password, can only be validated properly here.
33+
$rules = $this->getValidationRules();
34+
35+
if (! $this->validate($rules)) {
36+
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
37+
}
3738

38-
$credentials = $request->getPost(setting('Auth.validFields'));
39+
$credentials = $this->request->getPost(setting('Auth.validFields'));
3940
$credentials = array_filter($credentials);
40-
$credentials['password'] = $request->getPost('password');
41-
$remember = (bool) $request->getPost('remember');
41+
$credentials['password'] = $this->request->getPost('password');
42+
$remember = (bool) $this->request->getPost('remember');
4243

4344
// Attempt to login
4445
$result = auth('session')->remember($remember)->attempt($credentials);
@@ -56,11 +57,23 @@ public function loginAction()
5657
}
5758

5859
/**
59-
* Logs the current user out.
60+
* Returns the rules that should be used for validation.
6061
*
61-
* @return Response|string
62+
* @return string[]
63+
*/
64+
protected function getValidationRules(): array
65+
{
66+
return setting('Validation.login') ?? [
67+
//'username' => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
68+
'email' => 'required|max_length[254]|valid_email',
69+
'password' => 'required',
70+
];
71+
}
72+
73+
/**
74+
* Logs the current user out.
6275
*/
63-
public function logoutAction()
76+
public function logoutAction(): RedirectResponse
6477
{
6578
auth()->logout();
6679

src/Models/UserModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function addToDefaultGroup(User $user): void
118118
public function fake(Generator &$faker): User
119119
{
120120
return new User([
121-
'username' => $faker->userName,
121+
'username' => str_replace('.', ' ', $faker->userName),
122122
'active' => true,
123123
]);
124124
}

src/Views/login.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@
1010
<h5 class="card-title mb-5"><?= lang('Auth.login') ?></h5>
1111

1212
<?php if (session('error') !== null) : ?>
13-
<div class="alert alert-danger" role="alert"><?= session('error') ?></div>
13+
<div class="alert alert-danger" role="alert"><?= session('error') ?></div>
14+
<?php elseif (session('errors') !== null) : ?>
15+
<div class="alert alert-danger" role="alert">
16+
<?php if (is_array(session('errors'))) : ?>
17+
<?php foreach (session('errors') as $error) : ?>
18+
<?= $error ?>
19+
<br>
20+
<?php endforeach ?>
21+
<?php else : ?>
22+
<?= session('errors') ?>
23+
<?php endif ?>
24+
</div>
1425
<?php endif ?>
1526

1627
<?php if (session('message') !== null) : ?>

tests/Controllers/LoginTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeIgniter\Test\DatabaseTestTrait;
99
use CodeIgniter\Test\FeatureTestTrait;
1010
use Config\Services;
11+
use Config\Validation;
1112
use Tests\Support\FakeUser;
1213
use Tests\Support\TestCase;
1314

@@ -73,6 +74,7 @@ public function testLoginActionEmailSuccess(): void
7374
'password' => 'secret123',
7475
]);
7576

77+
$result->assertSessionHas('user', ['id' => 1]);
7678
$result->assertStatus(302);
7779
$result->assertRedirect();
7880
$this->assertSame(site_url(), $result->getRedirectUrl());
@@ -109,6 +111,15 @@ public function testAfterLoggedInNotDesplayLoginPage(): void
109111

110112
public function testLoginActionUsernameSuccess(): void
111113
{
114+
// Change the validation rules
115+
$config = new class () extends Validation {
116+
public $login = [
117+
'username' => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
118+
'password' => 'required',
119+
];
120+
};
121+
Factories::injectMock('config', 'Validation', $config);
122+
112123
$this->user->createEmailIdentity([
113124
'email' => 'foo@example.com',
114125
'password' => 'secret123',
@@ -119,6 +130,7 @@ public function testLoginActionUsernameSuccess(): void
119130
'password' => 'secret123',
120131
]);
121132

133+
$result->assertSessionHas('user', ['id' => 1]);
122134
$result->assertStatus(302);
123135
$result->assertRedirect();
124136
$this->assertSame(site_url(), $result->getRedirectUrl());

0 commit comments

Comments
 (0)