Created
October 16, 2020 13:25
-
-
Save blogcacanid/7980057332ccf812f5ccea4b807ec417 to your computer and use it in GitHub Desktop.
LoginController.php Login Dan Register System CodeIgniter 4
This file contains 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 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