Codeigniter login with codeigniter-bcrypt
I am using codeigniter-bcrypt from
https://github.com/dwightwatson/codeigniter-bcrypt, with codeigniter. I
have a form that is submitting post data to my main controller. I then
check via a model the DB for the record. I have used the
$hash = $this->bcrypt->hash_password($password);
To hash the password on account creation. And it works. Password is
properly hashed in DB. Now however I am unsure of where to use the reverse
to check if the password entered in the form to post is the same as the
DB's hashed password.
if ($this->bcrypt->check_password($password, $stored_hash))
{
// Password does match stored password.
}
else
{
// Password does not match stored password.
}
My code in my model is
function getUserByLogin($login, $password) {
$this->db->where('login',$login);
$this->db->where('password',$password);
$result = $this->getUsers();
if (count($result) > 0) {
return $result[0];
} else {
return null;
}
}
function getUsers() {
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
}
and my controller
if (isset($_POST['email']) && isset($_POST['password'])) {
$login = $_POST['email'];
$password = $_POST['password'];
$user = $this -> user_model -> getUserByLogin($login, $password);
$this -> saveUserToSession($user);
$loggedIn = ($user == null ? false : true);
}
Any help would be appreciated.
No comments:
Post a Comment