Reputation: 122
How can I do change Password Functionality using Restful API in Codeigniter.
I tried A code But It is Not Working and not catching in postman.
This is my controller Part:-
public function change_post()
{
try
{
$id = $this->input->post('id');
if ($id != '')
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old Password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New Password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new Password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == BOOL_F)
{
echo validation_errors();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($id, $oldPassword);
if($resultPas)
{
$this->response("password no match.", REST_Controller::HTTP_BAD_REQUEST);
}
else
{
$changeData = array('password'=>md5($newPassword));
$result = $this->user_model->changePassword($id, $changeData);
if($result > 0) { $this->response([
'message' => 'password changed successful.',
'data' => $change
], REST_Controller::HTTP_OK); }
else {$this->response("enter password first.", REST_Controller::HTTP_BAD_REQUEST);}
}
}
}
else
{
throw new Exception("The Data Already Register or The Data is Empty");
}
}
catch (Exception $e)
{
$error = array($e->getmessage());
$errormsg = json_encode($error);
echo $errormsg;
}
}
This is My Model Part:-
function matchOldPassword($id, $oldPassword)
{
$this->db->select('id, password');
$this->db->where('id', $id);
$query = $this->db->get('admins');
$admin = $query->result();
if(!empty($admin)){
if(md5($oldPassword, $admin[0]->password)){
return $admin;
} else {
return array();
}
} else {
return array();
}
}
function changePassword($id, $adminData)
{
$this->db->where('id', $id);
$this->db->update('admins', $adminData);
$this->db->last_query();
return TRUE;
}
But it is not working at all. How can i do correct Method to implement change password using Restful API in Codeigniter
Upvotes: 0
Views: 1597
Reputation: 1943
These are the mandatory files from that library to this application
application\libraries\REST_Controller.php
application\libraries\Format.php
application\config\rest.php
application\language\english\rest_controller_lang.php
Changes needed
$change
was not defined in your code
slight changes did in controller and model
Here the working code.
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/REST_Controller.php';
use \Restserver\Libraries\REST_Controller;
class Test extends REST_Controller {
function __construct(){
parent::__construct();
$this->load->model('user_model');
}
public function index_post()
{
try{
$id = $this->input->post('id');
if ($id != '')
{
$this->load->library('form_validation');
$this->form_validation->set_rules('oldPassword','Old Password','required|max_length[20]');
$this->form_validation->set_rules('newPassword','New Password','required|max_length[20]');
$this->form_validation->set_rules('cNewPassword','Confirm new Password','required|matches[newPassword]|max_length[20]');
if($this->form_validation->run() == false)
{
echo validation_errors();
}
else
{
$oldPassword = $this->input->post('oldPassword');
$newPassword = $this->input->post('newPassword');
$resultPas = $this->user_model->matchOldPassword($id, $oldPassword);
if($resultPas)
{
$this->set_response("password no match.", REST_Controller::HTTP_BAD_REQUEST);
}
else
{
$changeData = array('password'=>md5($newPassword));
$result = $this->user_model->changePassword($id, $changeData);
if($result > 0) { $this->set_response([
'message' => 'password changed successful.',
'data' => $changeData
], REST_Controller::HTTP_OK); }
else { $this->set_response("enter password first.", REST_Controller::HTTP_BAD_REQUEST); }
}
}
}
else
{
throw new Exception("The Data Already Register or The Data is Empty");
}
}
catch (Exception $e)
{
$error = array($e->getmessage());
$errormsg = json_encode($error);
echo $errormsg;
}
}
}
?>
Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function matchOldPassword($id, $oldPassword)
{
$this->db->select('id, password');
$this->db->where('id', $id);
$query = $this->db->get('admins');
$admin = $query->result();
if(!empty($admin) && (md5($oldPassword) == $admin[0]->password) ){
return false;
}
return true;
}
function changePassword($id, $adminData)
{
$this->db->where('id', $id);
$this->db->update('admins', $adminData);
$this->db->last_query();
return TRUE;
}
}
?>
How to access
method - post
url - http://hostname/Test57471803/change
Upvotes: 1