Advertisement
wildanfuady

Untitled

Oct 30th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. <?php
  2. class Auth extends CI_Controller
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. $this->load->model('auth_model');
  8. $this->load->library('form_validation');
  9. $this->load->library('session');
  10. }
  11.  
  12. public function index()
  13. {
  14. $this->load->view('auth/login');
  15. }
  16. public function loginForm()
  17. {
  18. $this->form_validation->set_rules('email', 'Email', 'required');
  19. $this->form_validation->set_rules('password', 'Password', 'required');
  20. if ($this->form_validation->run() == FALSE) {
  21. $errors = $this->form_validation->error_array();
  22. $this->session->set_flashdata('errors', $errors);
  23. $this->session->set_flashdata('input', $this->input->post());
  24. redirect('/');
  25. } else {
  26. $email = htmlspecialchars($this->input->post('email'));
  27. $pass = htmlspecialchars($this->input->post('password'));
  28. // 12345
  29. $cek_login = $this->auth_model->cek_login($email);
  30. if($cek_login == FALSE)
  31. {
  32. echo '<script>alert("Email yang Anda masukan salah.");window.location.href="'.base_url('/').'";</script>';
  33. } else {
  34. if(password_verify($pass, $cek_login->password)){
  35. // if the username and password is a match
  36. $this->session->set_userdata('id', $cek_login->user_id);
  37. $this->session->set_userdata('username', $cek_login->user_name);
  38. $this->session->set_userdata('name', $cek_login->name);
  39. $this->session->set_userdata('email', $cek_login->email);
  40. $this->session->set_userdata('status', $cek_login->status); // cek status
  41. $this->session->set_userdata('level', $cek_login->level);
  42.  
  43. redirect('/dashboard');
  44.  
  45. } else {
  46. echo '<script>alert("Email atau Password yang Anda masukan salah.");window.location.href="'.base_url('/').'";</script>';
  47. }
  48. }
  49. }
  50. }
  51. public function register()
  52. {
  53. $this->load->view('auth/register');
  54. }
  55. public function registerForm()
  56. {
  57. $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[15]|is_unique[users.username]');
  58. $this->form_validation->set_rules('name', 'Nama', 'required');
  59. $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]|valid_email');
  60. $this->form_validation->set_rules('password', 'Password', 'required|trim');
  61. $this->form_validation->set_rules('confrim_password', 'Konfirmasi Password', 'required|trim|matches[password]');
  62. if ($this->form_validation->run() == FALSE) {
  63. $errors = $this->form_validation->error_array();
  64. $this->session->set_flashdata('errors', $errors);
  65. $this->session->set_flashdata('input', $this->input->post());
  66. redirect('auth/register');
  67. } else {
  68. $username = $this->input->post('username');
  69. $name = $this->input->post('name');
  70. $email = $this->input->post('email');
  71. $password = $this->input->post('password');
  72. $pass = password_hash($password, PASSWORD_DEFAULT);
  73. $data = [
  74. 'username' => $username,
  75. 'name' => $name,
  76. 'email' => $email,
  77. 'password' => $pass,
  78. 'status' => 'Inactive',
  79. 'level' => 'User'
  80. ];
  81. $insert = $this->auth_model->register("users", $data);
  82. if($insert){
  83. echo '<script>alert("Sukses! Anda berhasil melakukan register. Silahkan login untuk mengakses data.");window.location.href="'.base_url('/').'";</script>';
  84. }
  85. }
  86. }
  87. public function logout()
  88. {
  89. $this->session->sess_destroy();
  90. echo '<script>
  91. alert("Sukses! Anda berhasil logout.");
  92. window.location.href="'.base_url().'";
  93. </script>';
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement