示例#1
0
 /**
  * API login user
  * @method POST
  * @param usn
  * @param psw
  *
  * @return array|null
  */
 function userLogin()
 {
     // Check if request method is POST and $_POST has some value data.
     if (empty($_POST)) {
         $this->setError('E403');
         return null;
     }
     $username = $_POST['usn'];
     $user_pass = $_POST['psw'];
     if ($user = get_user_by('login', $username)) {
         $check_password = wp_check_password($user_pass, $user->user_pass, $user->ID);
         if ($check_password) {
             $user_id = $user->ID;
         }
     } else {
         $this->setError('E101', 'Usename is incorrect.');
         return null;
     }
     if (isset($user_id) && $user_id) {
         /**
          * Login successful and prepare data for user.
          */
         $user_instant = new JS_User($user_id);
         $token = $user_instant->requestToken();
         $user = $user_instant->user;
         unset($user->data->user_pass);
         unset($user->data->user_activation_key);
         unset($user->data->user_status);
         $user->data->token = $token;
         $this->setSuccess('Login successfull');
         return $user->data;
     } else {
         $this->setError('E102', 'Password is incorrect');
     }
     return null;
 }