nhance_partner_be/app/Controllers/AgentAuthController.php

137 lines
4.9 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Models\AgentModel;
class AgentAuthController extends ResourceController
{
protected $format = 'json';
protected $myLogger;
protected $AgentModel;
public function __construct()
{
helper('jwt_helper');
helper('oauth_helper');
$this->myLogger = \Config\Services::mylogger();
$this->AgentModel = new AgentModel();
}
public function verifyAgentWithMobileNumber()
{
try {
$mobile = $this->request->getJSON()->mobile;
$agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->first();
if (isset($agentData['id']))
{
$result = ['agent_verification' => true ,'message' => "Verified Successfully"];
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
} else {
$result = ['agent_verification' => false , 'message' => "Verification Failed"];
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
}
} catch (\Throwable $th) {
return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500);
}
}
public function verifyAgentWithEmailId()
{
try {
$email = $this->request->getJSON()->email;
$agentData = $this->AgentModel->where('email', $email)->where('is_active', 1)->first();
if (isset($agentData['id'])) {
$otp = random_int(100000, 999999);
$update = $this->AgentModel->where('email', $email)->where('is_active', 1)->set(['email_otp' => $otp])->update();
if ($update) {
$subject = 'Nhance user verification - OTP';
$mail_content = $otp . ' is your verification code for Nhance.';
$EmailResult = send_login_otp( $email , $subject , $mail_content);
if ($EmailResult['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 {
$result = ['agent_verification' => false, 'message' => "User not found"];
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
}
} catch (\Throwable $th) {
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $th], 500);
}
}
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;
$email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null;
if (isset($mobile))
{
$agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->first();
} else {
$agentData = $this->AgentModel->where('email', $email)->where('is_active', 1)->first();
}
if ($agentData && $otp_verification == true || $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();
}
$result = generateJWT($agentData);
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
} else {
return $this->respond(['status' => 'failed','code' => 404,'data' => []],200);
}
} catch (\Exception $e) {
return $this->respond(['status' => 'failed','code' => 500,'data' =>[], 'error' => $e->getMessage()],500);
}
}
}