2

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);
}
keithjgrant
  • 12,421
  • 6
  • 54
  • 88
  • Can you add the view you used to the post so we can see the form as well? Also, did you try adding some debugging statements to make sure the username and password are correctly being passed in to the login function? The user is already created in the database, right? – Brian Riehman Apr 18 '10 at 21:36

1 Answers1

0

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.

keithjgrant
  • 12,421
  • 6
  • 54
  • 88