Kohana Auth callbacks for login
I'v开发者_如何学Pythone got the Kohana Auth up and running lovely except for one minor thing. Using the Model_User that extends Model_Auth_User, there are callbacks that are used for email not available and username not available. When logging in with correct username and wrong password, I get a username not available error, which is obvious because the user is trying to log in. Any way around this? I spose I could use JS to get round this since I'm using JSON for my error messages. Any idea?
My actual error is different: login.username.invalid. When my username is valid. My login controller:
class Controller_Login extends Controller_Main {
public function action_index(){
if ($_POST)
{
#Instantiate a new user
$user = ORM::factory('user');
#Check Auth
$status = $user->login($_POST);
#If the post data validates using the rules setup in the user model
if ($status)
{
#redirect to the user account
$json = array('redirect'=>'home');
$this->request->headers['Content-Type'] = 'application/json';
$this->request->response = json_encode($json);
}else
{
#Get errors for display in view
//encode errors here
$errors = $_POST->errors('login');
$this->request->headers['Content-Type'] = 'application/json';
$this->request->response = json_encode($errors);
}
}
}
}
it is a bug in default Model_Auth_User class, i'm talkin about this part:
83 if ($array->check())
84 {
85 // Attempt to load the user
86 $this->where($fieldname, '=', $array['username'])->find();
87
88 if ($this->loaded() AND Auth::instance()->login($this, $array['password'], $remember))
89 {
90 if (is_string($redirect))
91 {
92 // Redirect after a successful login
93 Request::instance()->redirect($redirect);
94 }
95
96 // Login is successful
97 $status = TRUE;
98 }
99 else
100 {
101 $array->error('username', 'invalid');
102 }
103 }
Look at line 101, there is source of your problems. If login with correct username and wrong password fails, error of wrong username raises. You can easily overload this method, by writing your own login function in your model of user. Moreover, you don't need to write the whole class, just extend the default Model_User and define a single login() method inside.
精彩评论