diff --git a/app/Config/Autoload.php b/app/Config/Autoload.php index 7bafd98..b09d2c4 100755 --- a/app/Config/Autoload.php +++ b/app/Config/Autoload.php @@ -90,5 +90,5 @@ class Autoload extends AutoloadConfig * * @var list */ - public $helpers = ['JwtHelper','common_helper','mail_helper','url_helper','status_helper']; + public $helpers = ['JwtHelper','common_helper','mail_helper','url_helper','status_helper','sms_helper']; } diff --git a/app/Controllers/AgentAuthController.php b/app/Controllers/AgentAuthController.php index 620cde2..abdc6ba 100644 --- a/app/Controllers/AgentAuthController.php +++ b/app/Controllers/AgentAuthController.php @@ -31,8 +31,26 @@ class AgentAuthController extends ResourceController if (isset($agentData['id'])) { - $result = ['agent_verification' => true ,'message' => "Verified Successfully"]; - return $this->respond(['status' => 'success','code' => 200,'data' => $result],200); + + $otp = random_int(100000, 999999); + + $update = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->set(['email_otp' => $otp])->update(); + + if ($update) { + + $SMSResult = sendOtpSms($mobile,$otp); + + if ($SMSResult['status'] == 'success') { + $result = ['agent_verification' => true, 'message' => "Verified Successfully"]; + return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200); + } else { + $result = ['agent_verification' => false, 'message' => "Mail sending failed , try again"]; + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200); + } + } else { + $result = ['agent_verification' => false, 'message' => "Verification failed , try again"]; + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200); + } } else { @@ -89,28 +107,24 @@ class AgentAuthController extends ResourceController public function getVerifiedAgentData() { try { - - $otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null; - $mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null; - $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null; + $mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null; $email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null; + $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null; if (isset($mobile)) { - $agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->first(); + $agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->where('email_otp', $otp)->first(); } else { $agentData = $this->AgentModel->where('email', $email)->where('email_otp', $otp)->where('is_active', 1)->first(); } - if ($agentData && $otp_verification == true || $agentData && isset($this->request->getJSON()->otp) ) { + if ($agentData && isset($this->request->getJSON()->otp) ) { - if(isset($this->request->getJSON()->otp)){ - $this->AgentModel->where('id', $agentData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update(); - } + $this->AgentModel->where('id', $agentData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update(); $result = generateJWT($agentData); diff --git a/app/Controllers/StaffAuthController.php b/app/Controllers/StaffAuthController.php index 88e450c..8852b5b 100644 --- a/app/Controllers/StaffAuthController.php +++ b/app/Controllers/StaffAuthController.php @@ -31,8 +31,25 @@ class StaffAuthController extends ResourceController if (isset($StaffData['id'])) { - $result = ['Staff_verification' => true ,'message' => "Verified Successfully"]; - return $this->respond(['status' => 'success','code' => 200,'data' => $result],200); + $otp = random_int(100000, 999999); + + $update = $this->StaffModel->where('mobile', $mobile)->where('is_active', 1)->set(['email_otp' => $otp])->update(); + + if ($update) { + + $SMSResult = sendOtpSms($mobile,$otp); + + if ($SMSResult['status'] == 'success') { + $result = ['Staff_verification' => true, 'message' => "Verified Successfully"]; + return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200); + } else { + $result = ['Staff_verification' => false, 'message' => "Mail sending failed , try again"]; + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200); + } + } else { + $result = ['Staff_verification' => false, 'message' => "Verification failed , try again"]; + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200); + } } else { @@ -89,29 +106,25 @@ class StaffAuthController extends ResourceController public function getVerifiedStaffData() { try { - - $otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null; + $mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null; - - $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null; $email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null; + $otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null; if (isset($mobile)) { - $StaffData = $this->StaffModel->where('mobile', $mobile)->where('is_active', 1)->first(); + $StaffData = $this->StaffModel->where('mobile', $mobile)->where('is_active', 1)->where('email_otp', $otp)->first(); } else { $StaffData = $this->StaffModel->where('email', $email)->where('email_otp', $otp)->where('is_active', 1)->first(); } - if ($StaffData && $otp_verification == true || $StaffData && isset($this->request->getJSON()->otp) ) { - - if(isset($this->request->getJSON()->otp)){ - $this->StaffModel->where('id', $StaffData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update(); - } - + if ($StaffData && isset($this->request->getJSON()->otp) ) { + + $this->StaffModel->where('id', $StaffData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update(); + $result = generateJWT($StaffData); return $this->respond(['status' => 'success','code' => 200,'data' => $result],200); diff --git a/app/Helpers/sms_helper.php b/app/Helpers/sms_helper.php new file mode 100644 index 0000000..c4fa87a --- /dev/null +++ b/app/Helpers/sms_helper.php @@ -0,0 +1,40 @@ + $apiKey, + 'MobileNo' => $mobile, + 'SenderID' => $senderId, + 'Message' => $message, + 'ServiceName' => $serviceName, + 'DLTTemplateID' => $templateId, + ]); + + // Send request + $response = @file_get_contents($url); + + // If response failed + if ($response === FALSE) { + return ['status' => 'failed', 'message' => 'Failed to send SMS.']; + } + + + return ['status' => 'success', 'message' => 'OTP sent successfully', 'api_response' => $response]; + } +}