如何验证用于Android登录的哈希密码json

时间:2019-06-19 18:42:40

标签: php json

如何使用codeigniter api让用户从android应用登录。 JSON响应。

我想使用password_hash和password_verify方法来提高安全性。

我尝试在模型中使用password_verify,但收到错误1错误的密码。如果我调试我的应用程序,可以看到它尝试发布未加密的密码,但是我认为必须在Model中检查该密码。

控制器:

public function login() {
        $response = array("success" => 0, "error" => 0);
        if (isset($_POST['email']) && $_POST['email'] != '') {

            $device_token       = $_POST['device_token'];
            $email              = $_POST['email'];
            $password           = $_POST['password'];
            $device_type        = $_POST['device_type'];

            $data = $this->Registration_model->login($email,$password,$device_token,$device_type);
            if ($data) {

                $user_id         = $data['u']->id;
                $status_level    = $this->Freelancer_model->service_level($user_id);//Bronze,silver..
                $discount        = $this->Registration_model->discountDetails($user_id);

                if (!empty($discount)) {
                    $discount = $discount;
                } else {
                    $discount ='';
                }

                if ($data['u']->approve_status == 1) {
                    $response["error"]              = 0;    
                    $response["success"]            = 1;
                    $response["message"]            = "success";
                    $image = base_url().'upload/'.$data['u']->user_image;
                    $response["data"]["user_id"]            = $data['u']->id;
                    $response["data"]["user_image"]         = $image;
                    $response["data"]["user_type"]          = $data['u']->user_type;    
                    $response["data"]["referral_code"]      = $data['u']->referral_code;    
                    $response["data"]["device_token"]       = $data['u']->device_token; 
                    $response["data"]["company_name"]       = $data['u']->company_name;
                    $response["data"]["reg_no"]             = $data['u']->registration_no;
                    $response["data"]["first_name"]         = $data['u']->first_name;
                    $response["data"]["last_name"]          = $data['u']->last_name;
                    $response["data"]["dob"]                = $data['u']->dob;
                    $response["data"]["address"]            = $data['u']->address;
                    $response["data"]["lat"]                = $data['u']->lat;
                    $response["data"]["long"]               = $data['u']->long;         
                    $response["data"]["mobile"]             = $data['u']->mobile;
                    $response["data"]["email"]              = $data['u']->email;
                    $response["data"]["password"]           = $data['u']->password;
                    $response["data"]["gender"]             = $data['u']->gender;
                    $response["data"]["about"]              = $data['u']->about;                          
                    $response["data"]["address_acceptance"] = $data['u']->address_acceptance; 
                    $response["data"]["availability"]       = $data['u']->availability;     
                    $response["data"]["canceling_policy"]   = $data['u']->canceling_policy; 
                    $response["data"]["acceptance"]         = $data['u']->acceptance;       
                    $response["data"]["seen_status"]        = $data['u']->seen_status;            // 0=not, 1=yes
                    $response["data"]["approv_status"]      = $data['u']->approve_status;
                    $response["data"]["complete_serviceLevel"]= $status_level;      
                    $response["account"]                    = $data['a'];
                    $response["discount"]                   = $discount;
                    echo json_encode($response);
                } else {
                    $response["error"]          = 2;    
                    $response["success"]        = 0;
                    $response["message"]        = "User is not approved";
                    echo json_encode($response);
                }
            } else {
                $response["error"]          = 1;    
                $response["success"]        = 0;
                $response["message"]        = "Enter correct email and password";
                echo json_encode($response);
            }

        } else {
            $response["error"]=4;
            $response["message"]= "Access denied";
            echo json_encode($response);
        }
    } 

型号:

public function login($email, $password, $device_token, $device_type) {
        $r = $this->db->get_where('registration', array('email'=>$email, 'password'=>$password));
        $count = $r->num_rows();
        if ($count > 1) {
            $this->db->select('id,email,user_type');
            $this->db->from('registration');
            $this->db->where('email',$email);
            $cc = $this->db->get()->result();
            $response["error"]              = 0;    
            $response["success"]            = 1;
            $response["message"]            = "Success, Which account does you want to login?";
            $response["data"]               = $cc;
            echo json_encode($response);
            die();
        } else if ($count == 1) {
            $r1 = $r->row();
            $id = $r1->id;
            $this->db->where('id',$id);
            $update =$this->db->update('registration',array('device_token' => $device_token,'device_type' => $device_type));  
            //$id = $r1->id;
            $r2 = $this->db->get_where('accountdetails', array('user_id' => $id))->result();
            $data= array(
                    "u" =>$r1,
                    "a" =>$r2
                    );  
            return $data;                    
        } else {
            return false;
        }        
    }

0 个答案:

没有答案