Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Created October 16, 2020 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blogcacanid/7980057332ccf812f5ccea4b807ec417 to your computer and use it in GitHub Desktop.
Save blogcacanid/7980057332ccf812f5ccea4b807ec417 to your computer and use it in GitHub Desktop.
LoginController.php Login Dan Register System CodeIgniter 4
<?php namespace App\Controllers;
use App\Models\UserModel;
class LoginController extends BaseController
{
protected $model;
public function __construct()
{
$this->model = new UserModel();
$this->helpers = ['form', 'url'];
}
public function index()
{
if ($this->isLoggedIn()) {
return redirect()->to(base_url('home'));
}
$data = [
'title' => 'Login Dan Register System CodeIgniter 4'
];
return view('auth/login', $data);
}
private function isLoggedIn(): bool
{
if (session()->get('logged_in')) {
return true;
}
return false;
}
public function login()
{
$email = $this->request->getPost('email');
$password = $this->request->getPost('password');
$credentials = ['email' => $email];
$user = $this->model->where($credentials)
->first();
if (! $user) {
session()->setFlashdata('error', 'Email atau password anda salah.');
return redirect()->back();
}
$passwordCheck = password_verify($password, $user['password']);
if (! $passwordCheck) {
session()->setFlashdata('error', 'Email atau password anda salah.');
return redirect()->back();
}
$userData = [
'name' => $user['name'],
'email' => $user['email'],
'logged_in' => TRUE
];
session()->set($userData);
return redirect()->to(base_url('home'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment