Question

I'm trying to login with the following controller action, but my login attempt keeps failing (I get the 'invalid username and/or password' message). What am I doing wrong? I also tried the other method given in the examples in the auth documentation, Auth::instance()->login($user->username, $form->password);, but I get the same result. Kohana version is 2.3.4.

public function login() {
    $auth = Auth::instance();
    if ($auth->logged_in()) {
        url::redirect('/account/summary');
    }

    $view = new View('login');
    $view->username = '';
    $view->password = '';

    $post = $this->input->post();
    $form = new Validation($post);
    $form->pre_filter('trim', 'username')
         ->pre_filter('trim', 'password')
         ->add_rules('username', 'required');

    $failed = false;
    if (!empty($post) && $form->validate()) {
        $login = array(
            'username'  => $form->username,
            'password'  => $form->password,
        );
        if (ORM::factory('user')->login($login)) {
            url::redirect('/accounts/summary');
        } else {
            $view->username = $form->username;
            $view->message = in_array('required', $form->errors()) ?
                             'Username and password are required.' :
                             'Invalid username and/or password.';
        }
    }

    $view->render(true);
}
Was it helpful?

Solution

Figured out my problem... Something in my registration process is missing, because it's creating the user record but not the role-to-user assoc record. Login needs a specific role to log in to, or it won't work even with a valid username and password. Manually inserting the record allowed my to log in, so I'll just have to debug my registration action a bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top