Last active
October 8, 2025 03:34
-
-
Save nfaiz/7496d2925c53b65bf13546bc3f0f5356 to your computer and use it in GitHub Desktop.
auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Controllers; | |
| use App\Models\UserModel; | |
| class AuthController extends BaseController | |
| { | |
| public function login() | |
| { | |
| return view('auth/login'); | |
| } | |
| public function loginAction() | |
| { | |
| //$model = new UserModel(); | |
| $model = model(UserModel::class); | |
| $user = $model->where('email', $this->request->getPost('email'))->first(); | |
| if ($user && password_verify($this->request->getPost('password'), $user['password'])) { | |
| session()->set('user', [ | |
| 'id' => $user['id'], | |
| 'name' => $user['name'], | |
| 'email'=> $user['email'], | |
| 'role' => $user['role'] | |
| ]); | |
| return redirect()->to(site_url('dashboard')); | |
| } | |
| return redirect()->back()->with('error', 'Email atau password salah')->withInput(); | |
| } | |
| public function register() | |
| { | |
| return view('auth/register'); | |
| } | |
| public function registerAction() | |
| { | |
| $rules = [ | |
| 'name' => 'required|min_length[3]', | |
| 'email' => 'required|valid_email|is_unique[users.email]', | |
| 'password' => 'required|min_length[6]' | |
| ]; | |
| if (! $this->validate($rules)) { | |
| return view('auth/register', ['validation' => $this->validator]); | |
| } | |
| $model = new UserModel(); | |
| $model->save([ | |
| 'name' => $this->request->getPost('name'), | |
| 'email' => $this->request->getPost('email'), | |
| 'role' => 'user', | |
| 'password' => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT), | |
| ]); | |
| return redirect()->to(site_url('login'))->with('success', 'Pendaftaran berjaya. Sila login.'); | |
| } | |
| public function logout() | |
| { | |
| session()->destroy(); | |
| return redirect()->to(site_url('login')); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- app/Views/dashboard.php --> | |
| <?= $this->extend('template/bootstrap') ?> | |
| <?= $this->section('content') ?> | |
| <h2>Dashboard</h2> | |
| <div class="col-md-4"> | |
| <p>Welcome, <?= esc($user['name']) ?></p> | |
| <a href="<?= site_url('logout') ?>">Logout</a> | |
| </div> | |
| <?= $this->endSection() ?> | |
| <?= $this->section('sub_content') ?> | |
| <?= $this->endSection() ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Controllers; | |
| use App\Controllers\BaseController; | |
| use CodeIgniter\HTTP\ResponseInterface; | |
| class DashboardController extends BaseController | |
| { | |
| public function index() | |
| { | |
| $data['user'] = session()->get('user'); | |
| return view('dashboard', $data); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?= $this->extend('template/bootstrap') ?> | |
| <?= $this->section('content') ?> | |
| <h2>Login</h2> | |
| <div class="col-md-4"> | |
| <?php if (session()->getFlashdata('error')) : '' ?> | |
| <?= view_cell('BootstrapCell::AlertMessage', ['type' => 'danger', 'message' => session()->getFlashdata('error')]) ?> | |
| <?php endif; ?> | |
| <?php if (session()->getFlashdata('success')) : '' ?> | |
| <?= view_cell('BootstrapCell::AlertMessage', ['type' => 'success', 'message' => session()->getFlashdata('success')]) ?> | |
| <?php endif; ?> | |
| <form method="post"> | |
| <?= csrf_field() ?> | |
| <div class="mb-3"> | |
| <label for="inputName" class="form-label">Email</label> | |
| <input name="email" placeholder="Email" class="form-control"> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="inputName" class="form-label">Password</label> | |
| <input type="password" name="password" placeholder="Password" class="form-control"> | |
| </div> | |
| <button type="submit" class="btn btn-primary">Login</button> | |
| </form> | |
| </div> | |
| <?= $this->endSection() ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- app/Views/auth/register.php --> | |
| <?= $this->extend('template/bootstrap') ?> | |
| <?= $this->section('content') ?> | |
| <h2>Register</h2> | |
| <div class="col-md-4"> | |
| <?php if (isset($validation)) : '' ?> | |
| <?= view_cell('BootstrapCell::AlertMessage', ['type' => 'danger', 'message' => $validation->listErrors()]) ?> | |
| <?php endif; ?> | |
| <form method="post"> | |
| <?= csrf_field() ?> | |
| <div class="mb-3"> | |
| <label for="inputName" class="form-label">Name</label> | |
| <input name="name" id="inputName" placeholder="Name" value="<?= old('name') ?>" class="form-control"> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="inputEmail" class="form-label">Email address</label> | |
| <input name="email" id="inputEmail" placeholder="Email" value="<?= old('email') ?>" class="form-control"> | |
| </div> | |
| <div class="mb-3"> | |
| <label for="inputPassword" class="form-label">Password</label> | |
| <input type="password" name="password" placeholder="Password" class="form-control"> | |
| </div> | |
| <button type="submit" class="btn btn-secondary">Register</button> | |
| </form> | |
| </div> | |
| <?= $this->endSection() ?> | |
| <?= $this->section('sub_content') ?> | |
| <?= $this->endSection() ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $routes->get('register', 'AuthController::register'); | |
| $routes->post('register', 'AuthController::registerAction'); | |
| $routes->get('login', 'AuthController::login'); | |
| $routes->post('login', 'AuthController::loginAction'); | |
| $routes->get('logout', 'AuthController::logout'); | |
| $routes->get('dashboard', 'DashboardController::index'); //, ['filter' => 'authGuard'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment