views:

1000

answers:

4

Hi, i'm trying to learn the Kohana's Auth module but login method always return false.

Controller:

<?php defined('SYSPATH') OR die('No Direct Script Access');
class Controller_Auth extends Controller {
    public function action_index() {
        if($_POST) {
            $this->login();
        }

        $this->template = View::factory('login');
        echo $this->template;
    }

    private function login() {
    $user = ORM::factory('user');

        $data = array('username' => 'wilson', 'password' => '123');
        if(!$user->login($data)) {
            echo 'FAILED!';
        }
    }

    private function logout() {

    }
}    
?>

Model:

<?php defined('SYSPATH')  or die('No direct script access.');
class Model_User extends Model_Auth_User {
}
?>
A: 

I can't have it working either, it seems like the method login in User does not return what it says return. Anyone had this working in Kohana 3?

Actually Model_Auth_User is like this:

/**
     * Validates login information from an array, and optionally redirects
     * after a successful login.
     *
     * @param  array    values to check
     * @param  string   URI or URL to redirect to
     * @return boolean
     */
    public function login(array & $array, $redirect = FALSE)
    {
        $array = Validate::factory($array)
            ->filter(TRUE, 'trim')
            ->rules('username', $this->_rules['username'])
            ->rules('password', $this->_rules['password']);

        // Login starts out invalid
        $status = FALSE;

        if ($array->check())
        {
            // Attempt to load the user
            $this->where('username', '=', $array['username'])->find();

            if ($this->loaded() AND Auth::instance()->login($this, $array['password']))
            {
                if (is_string($redirect))
                {
                    // Redirect after a successful login
                    Request::instance()->redirect($redirect);
                }

                // Login is successful
                $status = TRUE;
            }
            else
            {
                $array->error('username', 'invalid');
            }
        }

        return $status;
    }
Sebas
A: 

Here, Auth::instance()->login(...) returns always false :(

Thomas
A: 

You're doing it wrong.

To login a user, do something like this :

$auth = Auth::instance();
if ($auth->login($_POST['username'], $_POST['password']))
{
      echo 'hello, '.$auth->$_POST['username'];
}
else
{
      echo 'login failed!';
}

Also, uncomment the auth module line in the bootstrap of your application :

'auth'       => MODPATH.'auth',       // Basic authentication
yoda
A: 

I didn't add rules to the user. For more information, see this link.

Thank you guys and sorry about that :)

Thomas