Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
- class Auth extends CI_Controller
- {
- public function __construct()
- {
- parent::__construct();
- $this->load->library('form_validation');
- }
- public function index()
- {
- if ($this->session->userdata('email')) {
- redirect('user');
- }
- $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
- $this->form_validation->set_rules('password', 'Password', 'trim|required');
- if ($this->form_validation->run() == false) {
- $data['tittle'] = 'WPU user login';
- $this->load->view('templates/auth_header', $data);
- $this->load->view('auth/login');
- $this->load->view('templates/auth_footer');
- } else {
- $this->_login();
- }
- }
- private function _login()
- {
- $email = $this->input->post('email', true);
- $password = $this->input->post('password', true);
- $user = $this->db->get_where('user', ['email' => $email])->row_array();
- // jika user ada
- if ($user) {
- // jika user aktif
- if ($user['is_active'] == 1) {
- // cek password
- if (password_verify($password, $user['password'])) {
- $data = [
- 'email' => $user['email'],
- 'role_id' => $user['role_id']
- ];
- $this->session->set_userdata($data);
- if ($user['role_id'] == 1) {
- redirect('admin');
- } else {
- redirect('user');
- }
- } else {
- $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Wrong password!</div>');
- redirect('auth');
- }
- } else {
- $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">This email has been not activated!</div>');
- redirect('auth');
- }
- } else {
- $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Email has not been registered!</div>');
- redirect('auth');
- }
- }
- public function register()
- {
- if ($this->session->userdata('email')) {
- redirect('user');
- }
- $this->form_validation->set_rules('name', 'Name', 'required|trim');
- $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|is_unique[user.email]', [
- 'is_unique' => 'This email has alredy register!'
- ]);
- $this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
- 'matches' => 'The Password field does not match!'
- ]);
- $this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
- if ($this->form_validation->run() == false) {
- $data['tittle'] = 'WPU user registration';
- $this->load->view('templates/auth_header', $data);
- $this->load->view('auth/register');
- $this->load->view('templates/auth_footer');
- } else {
- $data = [
- 'name' => htmlspecialchars($this->input->post('name', true)),
- 'email' => htmlspecialchars($this->input->post('email', true)),
- 'image' => 'default.jpg',
- 'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
- 'role_id' => 2,
- 'is_active' => 0,
- 'date_created' => time()
- ];
- // $this->db->insert('user', $data);
- $this->_sendEmail();
- $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your account has been created</div>');
- redirect('auth');
- }
- }
- private function _sendEmail()
- {
- $config = [
- 'protocol' => 'smtp',
- 'smtp_host' => 'ssl://smpt.googlemail.com',
- 'smtp_user' => 'wpu.login@gmail.com',
- 'smtp_pass' => 'Avenged@23',
- 'smtp_port' => 465,
- 'mailtype' => 'html',
- 'charset' => 'utf-8',
- 'newline' => "\r\n"
- ];
- $this->load->library('email', $config);
- $this->email->from('wpu.login@gmail.com', 'Wep Programing UNPAS');
- $this->email->to('arifintajul4@gmail.com');
- $this->email->subject('Testing');
- $this->email->message('hello world');
- if ($this->email->send()) {
- return true;
- } else {
- echo $this->email->print_debugger();
- die;
- }
- }
- public function logout()
- {
- $this->session->unset_userdata('email');
- $this->session->unset_userdata('role_id');
- $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">You have been logged out!</div>');
- redirect('auth');
- }
- public function blocked()
- {
- $data['tittle'] = 'Access Blocked';
- $this->load->view('templates/header', $data);
- $this->load->view('auth/blocked');
- $this->load->view('templates/footer', $data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement