Search code examples
kohana-3.2kohana-auth

I can't log in in Kohana 3.2 ORM Auth module?


Well, thats a trouble. The code is simple:

public function action_index()
{

    $post = $this->request->post();

    if ($post) {    
        // if I type it like this, manually - it will work
        $success = Auth::instance()->login('admin','password');

    }

    if (isset($success) and $success) { echo "Пользователь залогинен"; }

}

Unfortunately it log in only a first record in the database, which is admin as by default config was in the table, If I create a new user. Like this:

$auth = Auth::instance();
        $user = new Model_User();
        $user->username = "Victor";
        $user->$auth->hash_password('psw123');
        $user->email = "[email protected]";
        $user->save();

And than use it like I said, only with a real data as

$post["email"] or $post["username"] with $post["password"]

code:

if ($post) {    
    // the values from posts: 'Victor' or '[email protected]` & 'psw123'
    $success = Auth::instance()->login('[email protected]','psw123');

}

it will not log in me.

upd I can't login as admin, but all working perfectly if I'll change the role to login (it's 1 in the database). But if the role will be set to 2 (it's an admin role) it will not accept me, even do not make an instance of Auth.

        $post = $this->request->post();

        $success = Auth::instance()->login($post['email'], $post['pass']);


        if ($success)
        {
            echo "SUCCESS!";
        }

Once again, if the role will be 2 (it means admin) this is not will Success me instead of login role.

What can be a reason of this trouble?


Solution

  • I'm assuming you're using a default ORM auth driver. You don't need to hash your password when saving a new user - it is done automatically by a filter in the model. So saving a new user should look something like that:

    $user = ORM::factory("user");
    $user->username = "Victor";
    $user->password = "psw123";
    $user->email = "[email protected]";
    $user->save();