Skip to content

Commit 187fcf1

Browse files
feat: basic auth
1 parent baec137 commit 187fcf1

File tree

21 files changed

+618
-3
lines changed

21 files changed

+618
-3
lines changed

app/Config/App.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class App extends BaseConfig
3636
*
3737
* @var string
3838
*/
39-
public $indexPage = 'index.php';
39+
public $indexPage = '';
4040

4141
/**
4242
* --------------------------------------------------------------------------
@@ -55,7 +55,7 @@ class App extends BaseConfig
5555
*
5656
* @var string
5757
*/
58-
public $uriProtocol = 'REQUEST_URI';
58+
public $uriProtocol = 'PATH_INFO';
5959

6060
/**
6161
* --------------------------------------------------------------------------

app/Config/Filters.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Config;
44

5+
use App\Filters\AuthenticationFilter;
56
use CodeIgniter\Config\BaseConfig;
67
use CodeIgniter\Filters\CSRF;
78
use CodeIgniter\Filters\DebugToolbar;
@@ -19,6 +20,7 @@ class Filters extends BaseConfig
1920
'csrf' => CSRF::class,
2021
'toolbar' => DebugToolbar::class,
2122
'honeypot' => Honeypot::class,
23+
'auth' => AuthenticationFilter::class,
2224
];
2325

2426
/**

app/Config/Routes.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$routes->setDefaultMethod('index');
2222
$routes->setTranslateURIDashes(false);
2323
$routes->set404Override();
24-
$routes->setAutoRoute(true);
24+
$routes->setAutoRoute(false);
2525

2626
/*
2727
* --------------------------------------------------------------------
@@ -33,6 +33,16 @@
3333
// route since we don't have to scan directories.
3434
$routes->get('/', 'Home::index');
3535

36+
$routes->get('/login', 'Auth\LoginController::index');
37+
$routes->post('/login', 'Auth\LoginController::auth');
38+
$routes->get('/register', 'Auth\RegisterController::index');
39+
$routes->post('/register', 'Auth\RegisterController::register');
40+
$routes->post('/logout', 'Auth\LogoutController::logout');
41+
42+
$routes->group('dashboard', ['filter' => 'auth'], function ($routes) {
43+
$routes->get('/', 'DashboardController::index');
44+
});
45+
3646
/*
3747
* --------------------------------------------------------------------
3848
* Additional Routing

app/Config/Validation.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ class Validation
4040
//--------------------------------------------------------------------
4141
// Rules
4242
//--------------------------------------------------------------------
43+
public $register = [
44+
'name' => 'required|min_length[6]|max_length[255]',
45+
'username' => 'required|min_length[6]|max_length[255]',
46+
'password' => 'required|min_length[6]|max_length[255]',
47+
'password_confirmation' => 'required|min_length[6]|max_length[255]|matches[password]',
48+
];
49+
50+
public $login = [
51+
'username' => 'required|min_length[6]|max_length[255]',
52+
'password' => 'required|min_length[6]|max_length[255]',
53+
];
4354
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
use App\Controllers\BaseController;
6+
use App\Services\Auth\LogInUser;
7+
use Config\Services;
8+
9+
class LoginController extends BaseController
10+
{
11+
public function index()
12+
{
13+
return view('auth/login');
14+
}
15+
16+
public function auth()
17+
{
18+
$validator = Services::validation();
19+
$validator->run($this->request->getPost(), 'login');
20+
21+
if ($validator->getErrors()) {
22+
return redirect()->back()->with('errors', $validator->getErrors());
23+
}
24+
25+
try {
26+
new LogInUser($this->request);
27+
} catch (\Throwable $th) {
28+
return redirect()->back()->with('error', 'An error has occured: ' . $th->getMessage());
29+
}
30+
31+
return redirect()->to('/dashboard')->with('success', 'You have successfully logged in.');
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
use App\Controllers\BaseController;
6+
use App\Services\Auth\LogOutUser;
7+
8+
class LogoutController extends BaseController
9+
{
10+
public function logout()
11+
{
12+
try {
13+
new LogOutUser();
14+
} catch (\Throwable $th) {
15+
return redirect()->back()->with('error', 'An error occured: ' . $th->getMessage());
16+
}
17+
18+
return redirect()->to('/login')->with('success', 'Logged out successfully.');
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
use App\Controllers\BaseController;
6+
use App\Services\Auth\RegisterUser;
7+
use CodeIgniter\Config\Services;
8+
9+
class RegisterController extends BaseController
10+
{
11+
public function index()
12+
{
13+
return view('auth/register');
14+
}
15+
16+
public function register()
17+
{
18+
$validator = Services::validation();
19+
$validator->run($this->request->getPost(), 'register');
20+
21+
if ($validator->getErrors()) {
22+
return redirect()->back()->with('errors', $validator->getErrors());
23+
}
24+
25+
try {
26+
new RegisterUser($this->request->getPost());
27+
} catch (\Throwable $th) {
28+
return redirect()->back()->with('error', 'An error has occured: ' . $th->getMessage());
29+
}
30+
31+
return redirect()->to('/login')->with('success', 'You have successfully registered. Please login.');
32+
}
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Controllers\BaseController;
6+
7+
class DashboardController extends BaseController
8+
{
9+
public function index()
10+
{
11+
return view('dashboard/index');
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Database\Migrations;
4+
5+
use CodeIgniter\Database\Migration;
6+
7+
class CreateUsersTable extends Migration
8+
{
9+
public function up()
10+
{
11+
$this->forge->addField([
12+
'id INT(11) PRIMARY KEY AUTO_INCREMENT',
13+
'name VARCHAR(255) NOT NULL',
14+
'username VARCHAR(255) NOT NULL',
15+
'password VARCHAR(255) NOT NULL',
16+
'role VARCHAR(255) NOT NULL DEFAULT "user"',
17+
'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()',
18+
'updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP()',
19+
]);
20+
21+
$this->forge->createTable('users');
22+
}
23+
24+
public function down()
25+
{
26+
$this->forge->dropTable('users');
27+
}
28+
}

app/Filters/AuthenticationFilter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Filters;
4+
5+
use CodeIgniter\Filters\FilterInterface;
6+
use CodeIgniter\HTTP\RequestInterface;
7+
use CodeIgniter\HTTP\ResponseInterface;
8+
9+
class AuthenticationFilter implements FilterInterface
10+
{
11+
/**
12+
* Do whatever processing this filter needs to do.
13+
* By default it should not return anything during
14+
* normal execution. However, when an abnormal state
15+
* is found, it should return an instance of
16+
* CodeIgniter\HTTP\Response. If it does, script
17+
* execution will end and that Response will be
18+
* sent back to the client, allowing for error pages,
19+
* redirects, etc.
20+
*
21+
* @param RequestInterface $request
22+
* @param array|null $arguments
23+
*
24+
* @return mixed
25+
*/
26+
public function before(RequestInterface $request, $arguments = null)
27+
{
28+
if (!session()->user) {
29+
return redirect()->to('/login')->with('error', 'You have to be logged in.');
30+
}
31+
}
32+
33+
/**
34+
* Allows After filters to inspect and modify the response
35+
* object as needed. This method does not allow any way
36+
* to stop execution of other after filters, short of
37+
* throwing an Exception or Error.
38+
*
39+
* @param RequestInterface $request
40+
* @param ResponseInterface $response
41+
* @param array|null $arguments
42+
*
43+
* @return mixed
44+
*/
45+
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
46+
{
47+
//
48+
}
49+
}

0 commit comments

Comments
 (0)