tags:

views:

519

answers:

1

For those familiar with the Auth module in Kohana, I can't login a user. I can create a user fine, but apparently the hashes aren't matching. I've used the provide MySql schema to create the db and I'm using the the modules models.

Here's my create a user code:

    public function user_create()
 {
  $user = ORM::factory('user');
  $user->username = "user";

  $this->auth = Auth::instance();

  $user->email = "[email protected]";
  $user->password = $this->auth->hash_password('admin');
  $user->add(ORM::factory('role', 'login'));
  $user->add(ORM::factory('role', 'admin'));

  if ($user->save())
   {
    $this->template->title = "user saved";
    $this->template->content = "user saved";
   }
  }

It creates a user with a hashed password and gives it the proper login/admin roles. Everything looks fine in the DB. Here's my login code. I skipped the begining part that checks if the user is logged in.

            $user = $this->input->post('username');
        $password = $this->input->post('password');

        if(!empty($user))
            {
                $find_user = ORM::factory('user')->where('username', $user)->find();
                $username = $find_user->username;

                $this->auth = Auth::instance();

                if($this->auth->login($username, $password))
                    {
                        $error = "logged in";
                    }
                else
                    {
                        $error = "not logged in at all";
                    }
            }

        $this->template->content = new View('admin/login_view');
        $this->template->content->user_info = $username . " " . $password;
        $this->template->title = "Login Admin";
        $this->template->content->bind('error', $error);

It always returns "not logged in at all". I verify I entered the correct username and password, but it doesn't log in. I cannot figure out why. I'm using the built in hash_password function to create the password and I've followed the docs, but I can't spot the error. Any help?

+3  A: 

Hey there,

The Auth module in kohana automatically hashes the password when you set it via the __set() method. So in order for you to store you password just do like so:

public function user_create()
    {
            $user = ORM::factory('user');
            $user->username = "user";

            $this->auth = Auth::instance();

            $user->email = "[email protected]";
            $user->password = 'admin';
...

Hope that helps. If you wanted to check out the auth module (models/auth_user.php) you can see it hashing the password:

public function __set($key, $value)
{
 if ($key === 'password')
 {
  // Use Auth to hash the password
  $value = Auth::instance()->hash_password($value);
 }

 parent::__set($key, $value);
}
Jon
Thanks Jon, that's just what I needed! I should have realized that when I looked at the Auth_Model.
anthony
Why don't you accept this answer (click on the big tick mark) and give Jon the rep point he deserves ...
Lukman