Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Auth extends CI_Controller
- {
- public function __construct()
- {
- parent::__construct();
- $this->load->model('auth_model');
- $this->load->library('form_validation');
- $this->load->library('session');
- }
- public function index()
- {
- $this->load->view('auth/login');
- }
- public function loginForm()
- {
- $this->form_validation->set_rules('email', 'Email', 'required');
- $this->form_validation->set_rules('password', 'Password', 'required');
- if ($this->form_validation->run() == FALSE) {
- $errors = $this->form_validation->error_array();
- $this->session->set_flashdata('errors', $errors);
- $this->session->set_flashdata('input', $this->input->post());
- redirect('/');
- } else {
- $email = htmlspecialchars($this->input->post('email'));
- $pass = htmlspecialchars($this->input->post('password'));
- // 12345
- $cek_login = $this->auth_model->cek_login($email);
- if($cek_login == FALSE)
- {
- echo '<script>alert("Email yang Anda masukan salah.");window.location.href="'.base_url('/').'";</script>';
- } else {
- if(password_verify($pass, $cek_login->password)){
- // if the username and password is a match
- $this->session->set_userdata('id', $cek_login->user_id);
- $this->session->set_userdata('username', $cek_login->user_name);
- $this->session->set_userdata('name', $cek_login->name);
- $this->session->set_userdata('email', $cek_login->email);
- $this->session->set_userdata('status', $cek_login->status); // cek status
- $this->session->set_userdata('level', $cek_login->level);
- redirect('/dashboard');
- } else {
- echo '<script>alert("Email atau Password yang Anda masukan salah.");window.location.href="'.base_url('/').'";</script>';
- }
- }
- }
- }
- public function register()
- {
- $this->load->view('auth/register');
- }
- public function registerForm()
- {
- $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[15]|is_unique[users.username]');
- $this->form_validation->set_rules('name', 'Nama', 'required');
- $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]|valid_email');
- $this->form_validation->set_rules('password', 'Password', 'required|trim');
- $this->form_validation->set_rules('confrim_password', 'Konfirmasi Password', 'required|trim|matches[password]');
- if ($this->form_validation->run() == FALSE) {
- $errors = $this->form_validation->error_array();
- $this->session->set_flashdata('errors', $errors);
- $this->session->set_flashdata('input', $this->input->post());
- redirect('auth/register');
- } else {
- $username = $this->input->post('username');
- $name = $this->input->post('name');
- $email = $this->input->post('email');
- $password = $this->input->post('password');
- $pass = password_hash($password, PASSWORD_DEFAULT);
- $data = [
- 'username' => $username,
- 'name' => $name,
- 'email' => $email,
- 'password' => $pass,
- 'status' => 'Inactive',
- 'level' => 'User'
- ];
- $insert = $this->auth_model->register("users", $data);
- if($insert){
- echo '<script>alert("Sukses! Anda berhasil melakukan register. Silahkan login untuk mengakses data.");window.location.href="'.base_url('/').'";</script>';
- }
- }
- }
- public function logout()
- {
- $this->session->sess_destroy();
- echo '<script>
- alert("Sukses! Anda berhasil logout.");
- window.location.href="'.base_url().'";
- </script>';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement