Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Library for handling all user-related operations
- *
- */
- class Auth
- {
- private $CI = null;
- function __construct(){
- $this->CI = & get_instance();
- $this->CI->load->library('native_session');
- }
- /**
- * Handles login operation
- *
- * @param string $email
- * @param string $password
- * @return mixed
- */
- function login($email = null, $password = null) {
- if (is_null($email) && is_null($password)) {
- $email = $this->CI->input->post('email');
- $password = $this->CI->input->post('password');
- }
- $this->CI->load->model("UserModel");
- $user = $this->CI->UserModel->findUser($email, $password);
- if ($user) {
- $this->CI->UserModel->logLastLogin($user->id, $this->CI->input->ip_address());
- return $this->setUserData($user);
- } else {
- return false;
- }
- }
- /**
- * Handles logout operation
- *
- */
- function logout() {
- $this->CI->native_session->sess_destroy();
- }
- /**
- * Set user's profile information into session
- *
- * @param array $user
- * @return array
- */
- public function setUserData($user) {
- unset($user["pasword"]);
- $this->CI->native_session->set_item('user', $user);
- $this->CI->native_session->set_item('loginTime', time());
- return $user;
- }
- /**
- * Retrieves user's information. Can return full array or just a single value by key
- *
- * @param string $key
- * @return mixed
- */
- function user($key = null) {
- $user = $this->CI->native_session->get_item("user");
- if ($user && !is_null($key)) {
- return isset($user->$key) ? $user->$key : null;
- } else {
- return $user;
- }
- }
- }
Add Comment
Please, Sign In to add comment