├── LICENSE ├── README.md └── application ├── config └── authit.php ├── controllers └── Auth.php ├── helpers └── authit_helper.php ├── libraries └── Authit.php ├── models └── Authit_model.php ├── test_emails └── testemails.db └── views └── auth ├── forgot_password.php ├── login.php ├── reset_password.php └── signup.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Ron Bailey 2 | 3 | This is free and unencumbered software released into the public domain. 4 | 5 | Anyone is free to copy, modify, publish, use, compile, sell, or 6 | distribute this software, either in source code form or as a compiled 7 | binary, for any purpose, commercial or non-commercial, and by any 8 | means. 9 | 10 | In jurisdictions that recognize copyright laws, the author or authors 11 | of this software dedicate any and all copyright interest in the 12 | software to the public domain. We make this dedication for the benefit 13 | of the public at large and to the detriment of our heirs and 14 | successors. We intend this dedication to be an overt act of 15 | relinquishment in perpetuity of all present and future rights to this 16 | software under copyright law. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | OTHER DEALINGS IN THE SOFTWARE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CodeIgniter Authit 2 | ================== 3 | 4 | A lightweight and flexible CodeIgniter 3 authentication library. It comes packaged with an example `Auth` class and related views so that you can get your CI project up and running in minutes. Test/View, sent emails with an email preview that allows you to view the last sent email by going to ...auth/sentemails. See config/authit to turn on for development, off for production, and on each authentication action you want it on, already enabled on password reset functionality by default. 5 | 6 | Requirements 7 | ------------ 8 | 9 | * CodeIgniter 3.0+ 10 | * PHP 5.5+ 11 | * MySQL 12 | 13 | Installation 14 | ------------ 15 | 16 | 1. Download and unpack the contents of the application folder to your CodeIgniter project. 17 | 2. That's it! Visit `/auth` to signup and login. 18 | 3. If you want you can edit `application/config/authit.php` to change some settings, but the defaults are fine. 19 | 20 | Usage 21 | ----- 22 | 23 | See [example Auth controller](application/controllers/Auth.php) which provides a functioning example of login, sign up, logout and forget/reset password. The Authit library provides several API methods: 24 | 25 | `logged_in()` - Returns `true` if the current user logged in, `false` otherwise. 26 | 27 | `login($email, $password)` - Attempts to login a user with a given `$email` and `$password`. Returns `true` if successful and `false` otherwise. 28 | 29 | `logout([$redirect = false])` - Logs out the current user (by destroying the session). Accepts an optional `$redirect` parameter to redirect to a given URI after logout. 30 | 31 | `signup($email, $password)` - Attempts to create a user with a given `$email` and `$password`. Returns `true` if successful and `false` otherwise. 32 | 33 | `reset_password($user_id, $new_password)` - Resets the password of the user with the given `$user_id`. 34 | 35 | The [Authit helper](application/helpers/authit_helper.php) includes the following helper functions: 36 | 37 | `logged_in()` - Returns `true` if the current user logged in, `false` otherwise (shortcut to the Authit library `logged_in()` function). 38 | 39 | `user([$key = ''])` - Returns the session data for the currently logged in user. If you specifiy a `$key` you can get specific info, for exmaple `user('id')` returns the currently logged in user ID. 40 | -------------------------------------------------------------------------------- /application/config/authit.php: -------------------------------------------------------------------------------- 1 | load->library('authit'); 18 | $this->load->helper('authit'); 19 | $this->config->load('authit'); 20 | 21 | $this->load->helper('url'); 22 | } 23 | 24 | public function index() 25 | { 26 | if(!logged_in()) redirect('auth/login'); 27 | 28 | // Redirect to your logged in landing page here 29 | redirect('auth/dash'); 30 | } 31 | 32 | /** 33 | * Login page 34 | */ 35 | public function login() 36 | { 37 | if(logged_in()) redirect('auth/dash'); 38 | 39 | $this->load->library('form_validation'); 40 | $this->load->helper('form'); 41 | $data['error'] = false; 42 | 43 | $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 44 | $this->form_validation->set_rules('password', 'Password', 'required'); 45 | 46 | if($this->form_validation->run()){ 47 | if($this->authit->login(set_value('email'), set_value('password'))){ 48 | // Redirect to your logged in landing page here 49 | redirect('auth/dash'); 50 | } else { 51 | $data['error'] = 'Your email address and/or password is incorrect.'; 52 | } 53 | } 54 | 55 | $this->load->view('auth/login', $data); 56 | } 57 | 58 | /** 59 | * Signup page 60 | */ 61 | public function signup() 62 | { 63 | // Redirect to your logged in landing page here 64 | if(logged_in()) redirect('auth/dash'); 65 | 66 | $this->load->library('form_validation'); 67 | $this->load->helper('form'); 68 | $data['error'] = ''; 69 | 70 | $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique['. $this->config->item('authit_users_table') .'.email]'); 71 | $this->form_validation->set_rules('password', 'Password', 'required|min_length['. $this->config->item('authit_password_min_length') .']'); 72 | $this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|matches[password]'); 73 | 74 | if($this->form_validation->run()){ 75 | if($this->authit->signup(set_value('email'), set_value('password'))){ 76 | $this->authit->login(set_value('email'), set_value('password')); 77 | 78 | // Do some post signup stuff like send a welcome email... 79 | 80 | 81 | // Redirect to your logged in landing page here 82 | redirect('auth/dash'); 83 | } else { 84 | $data['error'] = 'Failed to sign up with the given email address and password.'; 85 | } 86 | } 87 | 88 | $this->load->view('auth/signup', $data); 89 | } 90 | 91 | /** 92 | * Logout page 93 | */ 94 | public function logout() 95 | { 96 | if(!logged_in()) redirect('auth/login'); 97 | 98 | // Redirect to your logged out landing page here 99 | $this->authit->logout('/'); 100 | } 101 | 102 | /** 103 | * Example dashboard page 104 | */ 105 | public function dash() 106 | { 107 | if(!logged_in()) redirect('auth/login'); 108 | 109 | echo 'Hi, '. user('email') .'. You have successfully logged in. Logout'; 110 | } 111 | 112 | /** 113 | * Forgot password page 114 | */ 115 | public function forgot() 116 | { 117 | // Redirect to your logged in landing page here 118 | if(logged_in()) redirect('auth/dash'); 119 | 120 | $test_emails = $this->config->item('authit_test_emails'); 121 | 122 | $this->load->library('form_validation'); 123 | $this->load->helper('form'); 124 | $data['success'] = false; 125 | 126 | $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_exists'); 127 | 128 | if($this->form_validation->run()){ 129 | $email = $this->input->post('email'); 130 | $this->load->model('authit_model'); 131 | $user = $this->authit_model->get_user_by_email($email); 132 | $slug = md5($user->id . $user->email . date('Ymd')); 133 | 134 | $this->load->library('email'); 135 | 136 | $from = "'noreply@example.com', 'Example App'"; // Change these details 137 | $subject = 'Reset your Password'; 138 | $message = 'To reset your password please click the link below and follow the instructions: 139 | 140 | '. site_url('auth/reset/'. $user->id .'/'. $slug) .' 141 | 142 | If you did not request to reset your password then please just ignore this email and no changes will occur. 143 | 144 | Note: This reset code will expire after '. date('j M Y') .'.'; 145 | 146 | $this->email->from($from); 147 | $this->email->to($email); 148 | $this->email->subject($subject); 149 | $this->email->message($message); 150 | 151 | if ($test_emails) { 152 | $this->savemails("Password Reset", $this->email->protocol, $this->email->mailtype, $from, $email, $subject, $message); 153 | } else { 154 | $this->email->send(); 155 | } 156 | 157 | $data['success'] = true; 158 | } 159 | 160 | $this->load->view('auth/forgot_password', $data); 161 | } 162 | 163 | public function savemails($origin,$protocol,$mailtype,$from,$email,$subject,$message) 164 | { 165 | $obj = new stdClass(); 166 | $obj->origin = $origin; 167 | $obj->protocol = $protocol; 168 | $obj->mailtype = $mailtype; 169 | $obj->curentdate = date('r'); 170 | $obj->from = $from; 171 | $obj->email = $email; 172 | $obj->subject = $subject; 173 | $obj->message = $message; 174 | 175 | $emailobj = serialize($obj); 176 | $email_db = getcwd().DIRECTORY_SEPARATOR."application".DIRECTORY_SEPARATOR."test_emails".DIRECTORY_SEPARATOR."testemails.db"; 177 | if (is_writable($email_db)) { 178 | $fp = fopen($email_db,"w"); 179 | fwrite($fp,$emailobj); 180 | fclose($fp); 181 | } 182 | 183 | } 184 | 185 | public function sentemails() 186 | { 187 | //view emails 188 | $email_db = getcwd().DIRECTORY_SEPARATOR."application".DIRECTORY_SEPARATOR."test_emails".DIRECTORY_SEPARATOR."testemails.db"; 189 | if (file_exists($email_db)){ 190 | $emailobj = file_get_contents($email_db); 191 | $obj = unserialize($emailobj); 192 | if (!empty($obj)) { ?> 193 | 194 | 195 | 196 | 197 | 198 | Emails Sent 199 | 257 | 258 | 259 |
260 | 261 |

Email Preview

262 | 263 |
264 |

Details

265 | 271 | 272 |
273 |

To: email) ? $obj->email : ""); ?>

274 |

From: from) ? $obj->from : ""); ?>

275 | 276 |

Subject: subject) ? $obj->subject : ""); ?>

277 | 278 |

Message Body: message) ? $obj->message : ""); ?>

279 |
280 | 281 |
282 | 283 | 284 | 285 | load->model('authit_model'); 303 | 304 | if($this->authit_model->get_user_by_email($email)){ 305 | return true; 306 | } else { 307 | $this->form_validation->set_message('email_exists', 'We couldn\'t find that email address in our system.'); 308 | return false; 309 | } 310 | } 311 | 312 | /** 313 | * Reset password page 314 | */ 315 | public function reset() 316 | { 317 | // Redirect to your logged in landing page here 318 | if(logged_in()) redirect('auth/dash'); 319 | 320 | $this->load->library('form_validation'); 321 | $this->load->helper('form'); 322 | $data['success'] = false; 323 | 324 | $user_id = $this->uri->segment(3); 325 | if(!$user_id) show_error('Invalid reset code.'); 326 | $hash = $this->uri->segment(4); 327 | if(!$hash) show_error('Invalid reset code.'); 328 | 329 | $this->load->model('authit_model'); 330 | $user = $this->authit_model->get_user($user_id); 331 | if(!$user) show_error('Invalid reset code.'); 332 | $slug = md5($user->id . $user->email . date('Ymd')); 333 | if($hash != $slug) show_error('Invalid reset code.'); 334 | 335 | $this->form_validation->set_rules('password', 'Password', 'required|min_length['. $this->config->item('authit_password_min_length') .']'); 336 | $this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|matches[password]'); 337 | 338 | if($this->form_validation->run()){ 339 | $this->authit->reset_password($user->id, $this->input->post('password')); 340 | $data['success'] = true; 341 | } 342 | 343 | $this->load->view('auth/reset_password', $data); 344 | } 345 | 346 | } -------------------------------------------------------------------------------- /application/helpers/authit_helper.php: -------------------------------------------------------------------------------- 1 | load->library('authit'); 15 | 16 | return $CI->authit->logged_in(); 17 | } 18 | 19 | function user($key = '') 20 | { 21 | $CI =& get_instance(); 22 | $CI->load->library('session'); 23 | 24 | $user = $CI->session->userdata('user'); 25 | if($key && isset($user->$key)) return $user->$key; 26 | return $user; 27 | } 28 | 29 | /* End of file: authit_helper.php */ 30 | /* Location: application/helpers/authit_helper.php */ -------------------------------------------------------------------------------- /application/libraries/Authit.php: -------------------------------------------------------------------------------- 1 | CI =& get_instance(); 19 | 20 | $this->CI->load->database(); 21 | $this->CI->load->library('session'); 22 | $this->CI->load->model('authit_model'); 23 | $this->CI->config->load('authit'); 24 | } 25 | 26 | public function logged_in() 27 | { 28 | return $this->CI->session->userdata('logged_in'); 29 | } 30 | 31 | public function login($email, $password) 32 | { 33 | $user = $this->CI->authit_model->get_user_by_email($email); 34 | if($user){ 35 | if(password_verify($password, $user->password)){ 36 | unset($user->password); 37 | $this->CI->session->set_userdata(array( 38 | 'logged_in' => true, 39 | 'user' => $user 40 | )); 41 | $this->CI->authit_model->update_user($user->id, array('last_login' => date('Y-m-d H:i:s'))); 42 | return true; 43 | } 44 | } 45 | 46 | return false; 47 | } 48 | 49 | public function logout($redirect = false) 50 | { 51 | $this->CI->session->sess_destroy(); 52 | if($redirect){ 53 | $this->CI->load->helper('url'); 54 | redirect($redirect, 'refresh'); 55 | } 56 | } 57 | 58 | public function signup($email, $password) 59 | { 60 | $user = $this->CI->authit_model->get_user_by_email($email); 61 | if($user) return false; 62 | 63 | $password = password_hash($password, PASSWORD_DEFAULT); 64 | $this->CI->authit_model->create_user($email, $password); 65 | return true; 66 | } 67 | 68 | public function reset_password($user_id, $new_password) 69 | { 70 | $new_password = password_hash($new_password, PASSWORD_DEFAULT); 71 | $this->CI->authit_model->update_user($user_id, array('password' => $new_password)); 72 | } 73 | 74 | } 75 | 76 | /* End of file: Authit.php */ 77 | /* Location: application/libraries/Authit.php */ -------------------------------------------------------------------------------- /application/models/Authit_model.php: -------------------------------------------------------------------------------- 1 | load->database(); 20 | $this->config->load('authit'); 21 | 22 | $this->users_table = $this->config->item('authit_users_table'); 23 | 24 | if(!$this->db->table_exists($this->users_table)) $this->create_users_table(); 25 | } 26 | 27 | public function get_user($user_id) 28 | { 29 | $query = $this->db->get_where($this->users_table, array('id' => $user_id)); 30 | if($query->num_rows()) return $query->row(); 31 | return false; 32 | } 33 | 34 | public function get_user_by_email($email) 35 | { 36 | $query = $this->db->get_where($this->users_table, array('email' => $email)); 37 | if($query->num_rows()) return $query->row(); 38 | return false; 39 | } 40 | 41 | public function get_users($order_by = 'id', $order = 'asc', $limit = 0, $offset = 0) 42 | { 43 | $this->db->order_by($order_by, $order); 44 | if($limit) $this->db->limit($limit, $offset); 45 | $query = $this->db->get($this->users_table); 46 | return $query->result(); 47 | } 48 | 49 | public function get_user_count() 50 | { 51 | return $this->db->count_all($this->users_table); 52 | } 53 | 54 | public function create_user($email, $password) 55 | { 56 | $data = array( 57 | 'email' => filter_var($email, FILTER_SANITIZE_EMAIL), 58 | 'password' => $password, // Should be hashed 59 | 'created' => date('Y-m-d H:i:s') 60 | ); 61 | $this->db->insert($this->users_table, $data); 62 | return $this->db->insert_id(); 63 | } 64 | 65 | public function update_user($user_id, $data) 66 | { 67 | $this->db->where('id', $user_id); 68 | $this->db->update($this->users_table, $data); 69 | } 70 | 71 | public function delete_user($user_id) 72 | { 73 | $this->db->delete($this->users_table, array('id' => $user_id)); 74 | } 75 | 76 | private function create_users_table() 77 | { 78 | $this->load->dbforge(); 79 | $this->dbforge->add_field('id'); 80 | $this->dbforge->add_field('email VARCHAR(200) NOT NULL'); 81 | $this->dbforge->add_field('password VARCHAR(200) NOT NULL'); 82 | $this->dbforge->add_field('created DATETIME NOT NULL'); 83 | $this->dbforge->add_field('last_login DATETIME NULL'); 84 | $this->dbforge->create_table($this->users_table); 85 | } 86 | 87 | } 88 | 89 | /* End of file: Authit_model.php */ 90 | /* Location: application/models/Authit_model.php */ 91 | -------------------------------------------------------------------------------- /application/test_emails/testemails.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/trafficinc/CodeIgniter-Authit/6ab849c3aeaeaf74426c764fc7d1acb65668b43b/application/test_emails/testemails.db -------------------------------------------------------------------------------- /application/views/auth/forgot_password.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Forgot Password 4 | 5 | 6 | 7 |

Login

8 | 9 | Thank you. We have sent you an email with further instructions on how to reset your password.

'; 12 | } else { 13 | echo form_open(); 14 | echo form_label('Email Address', 'email') .'
'; 15 | echo form_input(array('name' => 'email', 'value' => set_value('email'))) .'
'; 16 | echo form_error('email'); 17 | echo form_submit(array('type' => 'submit', 'value' => 'Reset Password')); 18 | echo form_close(); 19 | } 20 | ?> 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /application/views/auth/login.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Login 4 | 5 | 6 | 7 |

Sign Up | Forgot Password?

8 | 9 | '. $error .'

'; 11 | echo form_open(); 12 | echo form_label('Email Address', 'email') .'
'; 13 | echo form_input(array('name' => 'email', 'value' => set_value('email'))) .'
'; 14 | echo form_error('email'); 15 | echo form_label('Password', 'password') .'
'; 16 | echo form_password(array('name' => 'password', 'value' => set_value('password'))) .'
'; 17 | echo form_error('password'); 18 | echo form_submit(array('type' => 'submit', 'value' => 'Login')); 19 | echo form_close(); 20 | ?> 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /application/views/auth/reset_password.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Reset Password 4 | 5 | 6 | 7 |

Login

8 | 9 | You have successfully reset your password.

'; 12 | } else { 13 | echo form_open(); 14 | echo form_label('Password', 'password') .'
'; 15 | echo form_password(array('name' => 'password', 'value' => set_value('password'))) .'
'; 16 | echo form_error('password'); 17 | echo form_label('Confirm Password', 'password_conf') .'
'; 18 | echo form_password(array('name' => 'password_conf', 'value' => set_value('password_conf'))) .'
'; 19 | echo form_error('password_conf'); 20 | echo form_submit(array('type' => 'submit', 'value' => 'Save New Password')); 21 | echo form_close(); 22 | } 23 | ?> 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /application/views/auth/signup.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sign Up 4 | 5 | 6 | 7 |

Login

8 | 9 | '. $error .'

'; 11 | echo form_open(); 12 | echo form_label('Email Address', 'email') .'
'; 13 | echo form_input(array('name' => 'email', 'value' => set_value('email'))) .'
'; 14 | echo form_error('email'); 15 | echo form_label('Password', 'password') .'
'; 16 | echo form_password(array('name' => 'password', 'value' => set_value('password'))) .'
'; 17 | echo form_error('password'); 18 | echo form_label('Confirm Password', 'password_conf') .'
'; 19 | echo form_password(array('name' => 'password_conf', 'value' => set_value('password_conf'))) .'
'; 20 | echo form_error('password_conf'); 21 | echo form_submit(array('type' => 'submit', 'value' => 'Sign Up')); 22 | echo form_close(); 23 | ?> 24 | 25 | 26 | 27 | --------------------------------------------------------------------------------