FEAT_PASSWORD_LOGIN_API

This commit is contained in:
VENKATESHWARAN 2025-11-10 14:14:26 +05:30
parent 13c9ece323
commit c473943c97
3 changed files with 330 additions and 8 deletions

View File

@ -434,8 +434,6 @@ $routes->cli('cli/new_gdrive_token', 'GoogleDriveController::generateNewGoogleDr
//Employee login api's
$routes->post("/employeeRest/verifyEmployeeNumber", "RestAuthenticationController::verifyEmployeeWithMobileNumber");
$routes->post("/employeeRest/getVerifiedUserData", "RestAuthenticationController::getVerifiedUserData");
$routes->post("/employeeRest/verifyMpin", "RestAuthenticationController::verifyMpin");
$routes->post("/employeeRest/checkMpin", "RestAuthenticationController::checkMpin");
$routes->post("/employeeRest/verifyEmployeeEmailId", "RestAuthenticationController::verifyEmployeeWithEmailId");
// $routes->post("/employeeRest/saveMpin", "RestAuthenticationController::saveMpin");
@ -450,12 +448,19 @@ $routes->group("/api", ["filter" => "authJWT"], function ($routes) {
$routes->post("getId", "RestAuthenticationController::getUserIdFromToken");
});
// MPIN api's
$routes->post("employeeRest/saveMpin", "RestAuthenticationController::saveMpin");
$routes->post("employeeRest/updateMpin", "RestAuthenticationController::updateMpin");
$routes->post("/employeeRest/verifyMpin", "RestAuthenticationController::verifyMpin");
$routes->post("/employeeRest/checkMpin", "RestAuthenticationController::checkMpin");
$routes->post("employeeRest/forgotMPIN", "RestAuthenticationController::forgotMPIN");
$routes->post("calculatePremium", "EmployeeRestController::calculatePremium");
$routes->post("employeeRest/updateMobileNumber", "RestAuthenticationController::updateMobileNumber");
// PASSWORD api's
$routes->post("employeeRest/savePassword", "RestAuthenticationController::savePassword");
$routes->post("employeeRest/changePassword", "RestAuthenticationController::changePassword");
$routes->post("employeeRest/verifyPassword", "RestAuthenticationController::verifyPassword");
$routes->post("employeeRest/verifyOtp", "RestAuthenticationController::verifyOtp");
// $routes->post("employeeRest/createOrUpdateEmployeePolicySiAmount", "EmployeeRestController::createOrUpdateEmployeePolicySiAmount");
// $routes->post("employeeRest/calculatePremium", "EmployeeRestController::calculatePremium");
@ -507,11 +512,12 @@ $routes->group("employeeRest", ["filter" => "authJWT"], function ($routes) {
$routes->post("hrFileUpload", "EmployeeRestController::hrFileUpload");
});
$routes->get("getEmployeeActiveOrInactivePolicy", "EmployeeRestController::getEmployeeActiveOrInactivePolicy");
$routes->get("sendPushNotification", "EmployeeRestController::sendPushNotification");
$routes->post("sendEmail", "EmployeeRestController::send_email");
$routes->get("getPolicyLevelEmployeeSummaryData", "EmployeeRestController::getPolicyLevelEmployeeSummaryData");
$routes->post("calculatePremium", "EmployeeRestController::calculatePremium");
$routes->get("getBackToEnrolledDetails", "EmployeeRestController::getBackToEnrolledDetails");
//crone job

View File

@ -1410,9 +1410,6 @@ class RestAuthenticationController extends AdminController
} catch (\Throwable $th) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - updateMobileNumber: Exception: " . $th->getMessage());
log_message('error', ' ');
log_message('error', ' ************************************* UPDATE MOBILE END **************************************** ');
log_message('error', ' ');
$errorData = [
'message' => $th->getMessage(),
@ -1433,4 +1430,322 @@ class RestAuthenticationController extends AdminController
], 500);
}
}
}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------
public function savePassword()
{
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Received payload = " . json_encode($this->request->getJSON() ?? []));
try {
$payload = $this->request->getJSON();
// print_r($payload); die;
$mobile_number = $payload->mobile_number ?? null;
$email_id = $payload->email_id ?? null;
$client_id = $payload->client_id ?? null;
$plain_password = $payload->password ?? null;
$confirm_password = $payload->confirm_password ?? null;
if (empty($plain_password) || empty($confirm_password)) {
return $this->respond(['status' => 'failed', 'code' => 400, 'message' => 'Both password cannot be empty'], 400);
}
if($plain_password !== $confirm_password){
return $this->respond(['status' => 'failed', 'code' => 400, 'message' => 'Password and confirm password not matched'], 400);
}
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number ]);
// print_r($empdata); die;
if(empty($empdata)){
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: No employee data found both PRE and POST");
$result = ['user_verification' => false , 'message' => "User not found"];
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
}
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: empdata = " . json_encode($empdata));
$employeeData = [];
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
$employeeData = $empdata['pre'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Using PRE data from empdata");
}
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
$payload->client_id = $empdata['post']['client_id'];
$payload->employee_id = $empdata['post']['employee_id'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Added client_id & employee_id to requestData to get the POST employee data");
}
if (isset($employeeData['employee_id'])) {
$id = $employeeData['employee_id'];
$hashedPassword = password_hash($plain_password, PASSWORD_DEFAULT);
$updateData = [
'password' => $hashedPassword,
];
$updated = $this->employeeModel->where('id', $id)->set($updateData)->update();
if ($updated) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Password saved successfully");
$this->callThirdPartyAPI($payload, 'savePassword');
$result = ['user_verification' => true, 'message' => "Password saved successfully"];
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Password save failed");
$result = ['user_verification' => true, 'message' => "Password not saved"];
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $result], 200);
}
} else {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Employee not found with email or mobile in the PRE DATABASE, falling back to third-party API");
$result = ['user_verification' => false, 'message' => "User not found"];
return $this->callThirdPartyAPI($payload, 'savePassword');
}
} catch (\Throwable $th) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Exception: " . $th->getMessage() . " --- Line: " . $th->getLine() . " --- Trace: " . $th->getTraceAsString());
$errorData = [
'message' => $th->getMessage(),
'file' => $th->getFile(),
'line' => $th->getLine(),
'code' => $th->getCode(),
'trace' => $th->getTraceAsString(),
'trace_array' => $th->getTrace(), // full array version (optional)
'function' => $th->getTrace()[0]['function'] ?? null,
'class' => $th->getTrace()[0]['class'] ?? null,
];
return $this->respond(['status' => 'failed', 'code' => 500, 'message' => $th->getMessage(), 'error_data' => $errorData], 500);
}
}
public function changePassword()
{
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - changePassword: Received payload = " . json_encode($this->request->getJSON() ?? []));
try {
$payload = $this->request->getJSON(true);
$mobile_number = $payload['mobile_number'] ?? null;
$email_id = $payload['email_id'] ?? null;
$client_id = $payload['client_id'] ?? null;
$old_password = $payload['old_password'] ?? null;
$new_password = $payload['new_password'] ?? null;
$confirm_password = $payload['confirm_password'] ?? null;
if (empty($new_password) || empty($confirm_password) || empty($old_password)) {
return $this->respond(['status' => 'failed', 'code' => 400, 'message' => 'old_password , New password and Confirm password are required' ], 400);
}
// Fetch employee details
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number ]);
// print_r($empdata); die;
if(empty($empdata)){
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: No employee data found both PRE and POST");
$result = ['user_verification' => false , 'message' => "User not found"];
return $this->respond(['status' => 'failed','code' => 404,'data' => $result],200);
}
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: empdata = " . json_encode($empdata));
$employeeData = [];
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
$employeeData = $empdata['pre'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Using PRE data from empdata");
}
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
$payload['client_id'] = $empdata['post']['client_id'];
$payload['employee_id'] = $empdata['post']['employee_id'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - savePassword: Added client_id & employee_id to requestData to get the POST employee data");
}
if (isset($employeeData['employee_id']) && password_verify($old_password, $employeeData['password'])) {
// Hash new password
// $newHashedPassword = password_hash($new_password, PASSWORD_DEFAULT);
$newHashedPassword = $new_password;
// Update password and clear OTP
$updated = $this->employeeModel->update($employeeData['employee_id'], [
'password' => $newHashedPassword,
]);
if ($updated) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - changePassword: Password updated successfully");
// Call the third-party API function
$this->callThirdPartyAPI($payload, 'changePassword');
return $this->respond(['status' => 'success','code' => 200,'message' => 'Password changed successfully' ], 200);
} else {
return $this->respond(['status' => 'failed','code' => 500,'message' => 'Failed to update password'], 500);
}
}else{
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - changePassword: Employee not verified - calling third-party API fallback to get the POST employee data");
// Call the third-party API function
$apiResponse = $this->callThirdPartyAPI($payload, 'changePassword');
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => "", 'post_enrollment' => json_decode($apiResponse, true)], 200);
}
} catch (\Throwable $th) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - changePassword: Exception: " . $th->getMessage());
return $this->respond(['status' => 'failed', 'code' => 500, 'message' => $th->getMessage()], 500);
}
}
public function verifyPassword()
{
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyPassword: Received payload = " . json_encode($this->request->getJSON() ?? []));
try {
$requestData = $this->request->getJSON(true);
$mobile_number = $requestData['mobile_number'] ?? null;
$email_id = $requestData['email_id'] ?? null;
$client_id = $requestData['client_id'] ?? null;
$password = $requestData['password'] ?? null;
if (!$password) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => "", 'message' => 'Password is required'], 400);
}
// 🔹 Fetch employee data
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number]);
// print_r($empdata); die;
if(empty($empdata)){
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyMpin: No employee data found both PRE and POST");
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => ""], 200);
}
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyMpin: empdata = " . json_encode($empdata));
$employeeData = [];
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
$employeeData = $empdata['pre'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyMpin: Using PRE data from empdata");
}
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
$requestData['client_id'] = $empdata['post']['client_id'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyMpin: Added client_id to requestData to get the POST employee data");
}
// 🔹 Verify password hash
if (isset($employeeData['employee_id']) && password_verify($password, $employeeData['password'])) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyPassword: Verified employee found");
// ✅ Log authentication info
$auth = HttpRequestHelper::getRequestInfo();
if ($auth) {
$this->authHistoryModel->insert([
'user_id' => $employeeData['employee_id'],
'user_type' => 'employee',
'ip' => $auth['ip'],
'platform' => $auth['platform'],
'broswer' => $auth['browser'],
]);
}
// ✅ Generate JWT token
unset($employeeData['employee_id']);
$employeeData['token_type'] = "pre";
$token = JWTToken::encode($employeeData);
// Call the third-party API function
$apiResponse = $this->callThirdPartyAPI($requestData, 'verifyPassword');
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $token, 'post_enrollment' => json_decode($apiResponse, true)], 200);
} else {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyPassword: Employee not verified - calling third-party API fallback to get the POST employee data");
// Call the third-party API function
$apiResponse = $this->callThirdPartyAPI($requestData, 'verifyPassword');
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => "", 'post_enrollment' => json_decode($apiResponse, true)], 200);
}
} catch (\Throwable $th) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyPassword: Exception: " . $th->getMessage() . " --- Line: " . $th->getLine());
$errorData = [
'message' => $th->getMessage(),
'file' => $th->getFile(),
'line' => $th->getLine(),
'code' => $th->getCode(),
'trace' => $th->getTraceAsString(),
'trace_array' => $th->getTrace(), // full array version (optional)
'function' => $th->getTrace()[0]['function'] ?? null,
'class' => $th->getTrace()[0]['class'] ?? null,
];
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => "", 'message' => $th->getMessage(), 'error_data' => $errorData], 500);
}
}
public function verifyOtp()
{
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: Received payload = " . json_encode($this->request->getJSON() ?? []));
try {
$requestData = $this->request->getJSON(true);
$mobile_number = $requestData['mobile_number'] ?? null;
$email_id = $requestData['email_id'] ?? null;
$client_id = $requestData['client_id'] ?? null;
$otp = $requestData['otp'] ?? null;
if (!$otp) {
return $this->respond(['status' => 'failed', 'code' => 400, 'data' => "", 'message' => 'OTP is required'], 400);
}
// 🔹 Fetch employee data
$empdata = RestAuthHelper::getPreAndPostDataByEmailOrMobile(['email_id' => $email_id, 'mobile_number' => $mobile_number]);
// print_r($empdata); die;
if(empty($empdata)){
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: No employee data found both PRE and POST");
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => ""], 200);
}
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: empdata = " . json_encode($empdata));
$employeeData = [];
if (isset($empdata['pre']) && !empty($empdata['pre'])) {
$employeeData = $empdata['pre'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: Using PRE data from empdata");
}
if (isset($empdata['post']['client_id']) && !empty($empdata['post']['client_id'])) {
$requestData['client_id'] = $empdata['post']['client_id'];
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: Added client_id to requestData to get the POST employee data");
}
// 🔹 Verify password hash
if ($employeeData && $employeeData['otp'] == $otp) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: Verified employee found");
$this->employeeModel->where('id', $employeeData['id'])->where('otp', $otp)->where('relationship', 'self')->set(['otp'=>null])->update();
return $this->respond(['status' => 'success', 'code' => 200, 'message' => "OTP verified succesfully"], 200);
} else {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: Employee not verified - calling third-party API fallback to get the POST employee data");
// Call the third-party API function
return $this->callThirdPartyAPI($requestData, 'verifyOtp');
}
} catch (\Exception $e) {
$this->myLogger->logme("error", "REST-AUTH-CONTROLLER - verifyOtp: Exception: " . $e->getMessage() . " --- Line: " . $e->getLine());
return $this->respond(['status' => 'failed', 'code' => 500, 'data' => "", 'message' => $e->getMessage()], 500);
}
}
}

View File

@ -45,6 +45,7 @@ class EmployeeModel extends Model
"mpin",
"is_mpin_skipped",
"is_biometric_enabled",
"password",
];
// Callbacks