Skip to content

Instantly share code, notes, and snippets.

@bmoore
Created July 2, 2012 15:29
Show Gist options
  • Save bmoore/3033798 to your computer and use it in GitHub Desktop.
Save bmoore/3033798 to your computer and use it in GitHub Desktop.
The authentication library.
<?php
class Auth
{
private $hasher;
private $session;
function __construct()
{
$this->hasher = new PasswordHash(8, false);
}
function hash_password($password)
{
return $this->hasher->HashPassword($password);
}
function check_password($password, $hash)
{
$result = false;
if ($hash) {
$result = $this->hasher->CheckPassword($password, $hash);
}
return $result;
}
function set_logged_in($user_id)
{
$this->load_session();
$this->session->set_userdata('user_id', $user_id);
}
function is_logged_in()
{
$this->load_session();
return $this->session->userdata('user_id') > 0;
}
function get_user_id()
{
$this->load_session();
return $this->session->userdata('user_id');
}
function set_logged_out()
{
$this->load_session();
$this->session->unset_userdata('user_id');
}
private function load_session()
{
if (!$this->session) {
$ci = get_instance();
$this->session = $ci->session;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment