0
我希望能夠通過電子郵件和密碼對數據庫中的用戶表進行身份驗證。我可以在Yii中做到這一點,但現在我希望能夠通過JSON格式的RESTful驗證相同的用戶。下面是我的驗證的Yii代碼,這是在Yii的defaut LoginForm的模型:Yii通過JSON格式的RESTful進行用戶身份驗證
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->email,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect email or password.');
}
}
/**
* Logs in the user using the given email and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->email,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
我怎樣才能做到這一點?
您是否找到解決方案 - 我也處於與您相同的位置 – Zabs