Initial baseline WACloud CI3 production
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
class Auth extends CI_Controller
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Auth_model');
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->load->helper('cookie');
|
||||
|
||||
// ✅ Jika sudah login (session aktif), langsung redirect saja
|
||||
if ($this->session->userdata('is_logged_in')) {
|
||||
if ($this->session->userdata('role') == 'admin') {
|
||||
redirect('dashboard');
|
||||
} else {
|
||||
redirect('chat');
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ Jika belum login tapi ada cookie remember_token
|
||||
$token = get_cookie('remember_token');
|
||||
if ($token) {
|
||||
$user = $this->db->get_where('users', ['remember_token' => $token])->row_array();
|
||||
if ($user) {
|
||||
// Buat session baru dari data user
|
||||
$this->session->set_userdata(array(
|
||||
'user_id' => $user['id'],
|
||||
'name' => $user['name'],
|
||||
'username' => $user['username'],
|
||||
'role' => $user['role'],
|
||||
'whatsapp_id' => $user['whatsapp_id'],
|
||||
'is_logged_in' => TRUE
|
||||
));
|
||||
|
||||
// Redirect sesuai role
|
||||
if ($user['role'] == 'admin') {
|
||||
redirect('dashboard');
|
||||
} else {
|
||||
redirect('chat');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Kalau tidak ada session dan cookie, tampilkan form login
|
||||
$this->load->view('login/layout');
|
||||
}
|
||||
|
||||
public function login_process()
|
||||
{
|
||||
$this->form_validation->set_rules('username', 'Username', 'required');
|
||||
$this->form_validation->set_rules('password', 'Password', 'required');
|
||||
|
||||
if ($this->form_validation->run() == FALSE) {
|
||||
|
||||
$this->load->view('login/layout');
|
||||
|
||||
} else {
|
||||
|
||||
$username = trim($this->input->post('username'));
|
||||
$password = $this->input->post('password');
|
||||
|
||||
// cek user berdasarkan username
|
||||
$user = $this->Auth_model->get_user($username);
|
||||
|
||||
// =========================
|
||||
// USERNAME TIDAK DITEMUKAN
|
||||
// =========================
|
||||
if (!$user) {
|
||||
|
||||
$this->session->set_flashdata(
|
||||
'error',
|
||||
'Username tidak ditemukan!'
|
||||
);
|
||||
|
||||
redirect('auth');
|
||||
}
|
||||
|
||||
// =========================
|
||||
// PASSWORD SALAH
|
||||
// =========================
|
||||
if (!password_verify($password, $user['password'])) {
|
||||
|
||||
$this->session->set_flashdata(
|
||||
'error',
|
||||
'Password yang anda masukkan salah!'
|
||||
);
|
||||
|
||||
redirect('auth');
|
||||
}
|
||||
|
||||
// =========================
|
||||
// LOGIN BERHASIL
|
||||
// =========================
|
||||
$session_data = array(
|
||||
'user_id' => $user['id'],
|
||||
'name' => $user['name'],
|
||||
'username' => $user['username'],
|
||||
'role' => $user['role'],
|
||||
'whatsapp_id' => $user['whatsapp_id'],
|
||||
'is_logged_in' => TRUE
|
||||
);
|
||||
|
||||
$this->session->set_userdata($session_data);
|
||||
|
||||
// Buat token unik untuk auto login
|
||||
$token = bin2hex(random_bytes(32));
|
||||
|
||||
// Simpan token ke database
|
||||
$this->db
|
||||
->where('id', $user['id'])
|
||||
->update('users', [
|
||||
'remember_token' => $token
|
||||
]);
|
||||
|
||||
// Simpan cookie 30 hari
|
||||
$cookie = array(
|
||||
'name' => 'remember_token',
|
||||
'value' => $token,
|
||||
'expire' => 60 * 60 * 24 * 30,
|
||||
'secure' => false, // ubah true jika HTTPS
|
||||
'httponly' => true
|
||||
);
|
||||
|
||||
$this->input->set_cookie($cookie);
|
||||
|
||||
// Redirect sesuai role
|
||||
if ($user['role'] == 'admin') {
|
||||
|
||||
redirect('dashboard');
|
||||
|
||||
} else {
|
||||
|
||||
redirect('chat');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
delete_cookie('remember_token');
|
||||
$this->session->sess_destroy();
|
||||
redirect('auth');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user