Kohana User Authentication

Always validate/filter user input. In Kohana validation can be performed on any array using the Validation class. Labels and rules can be attached to a Validation object by the array key, called a “field name”. User authentication and authorization is provided by the auth module. The auth module is included with Kohana, but needs to be enabled before you can use it. To enable, open your application/bootstrap.php file and modify the call to Kohana::modules by including the auth module.
 

public function action_login()
{
	if (HTTP_Request::POST == $this->request->method()) 
	{
		$post = $this->request->post();
		$validation = Validation::factory($post)
			->rule('username', 'not_empty')
			->rule('username', 'min_length', array(':value', 5))
			->rule('username', 'max_length', array(':value', 32))
			->rule('username', 'regex', array(':value', '/^[a-z_.]+$/'))
 
			->rule('password', 'not_empty')
			->rule('password', 'min_length', array(':value', 8))
			->rule('password', 'max_length', array(':value', 32))
			->rule('password', 'regex', array(':value', '/^[0-9a-zA-Z!@#$%^&*,-()_.]+$/'))
 
			->rule('token', 'not_empty')
			->rule('token', 'Security::check');
 
		if ($validation->check())
		{
			$success = Auth::instance()->login($post['username'], $post['password']);
			if ($success) 
			{
				HTTP::redirect('main/index');
			}
		}
		else
		{
			HTTP::redirect('home/index');
		}			
	}
	else
	{
		HTTP::redirect('home/index');
	}
}
%d bloggers like this: