From a3cb96ce74f822439c769900a9a11284a75b8c73 Mon Sep 17 00:00:00 2001 From: Gowtham M Date: Thu, 26 Feb 2026 15:27:26 +0530 Subject: [PATCH] HR token issue --- app/Controllers/RestAuthenticationController.php | 5 +++-- app/Filters/AuthJWT.php | 16 +++++++++++++--- app/Helpers/JWTToken.php | 8 ++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/Controllers/RestAuthenticationController.php b/app/Controllers/RestAuthenticationController.php index e4c474a1..ab536240 100755 --- a/app/Controllers/RestAuthenticationController.php +++ b/app/Controllers/RestAuthenticationController.php @@ -648,7 +648,8 @@ class RestAuthenticationController extends AdminController 'function' => $th->getTrace()[0]['function'] ?? null, 'class' => $th->getTrace()[0]['class'] ?? null, ]; - return $this->respond(['status' => 'failed','code' => 500,'data' => $th, 'error_data' => $errorData],500); + log_message('error', 'Error Data: ' . json_encode($errorData)); + return $this->respond(['status' => 'failed','code' => 500,'data' => $th],500); } } @@ -2345,7 +2346,7 @@ class RestAuthenticationController extends AdminController } $decoded = $result['decoded']; - $userId = $decoded['id'] ?? null; + $userId = $decoded['post_hr_id'] ?? null; if (!$userId) { return $this->respond([ diff --git a/app/Filters/AuthJWT.php b/app/Filters/AuthJWT.php index ec34d902..45f4517a 100755 --- a/app/Filters/AuthJWT.php +++ b/app/Filters/AuthJWT.php @@ -115,9 +115,19 @@ class AuthJWT implements FilterInterface } // Refresh sliding expiration - $model->update($id, [ - 'token_time_out' => time() + getenv('TOKENTIMEOUT') - ]); + $newExpiry = time() + getenv('TOKENTIMEOUT'); + + if (!isset($decoded['emp_code'])) { + // HR user: refresh token_time_out across all branches (same mobile/email) + // so switching branches doesn't cause an unexpected expiry + $field = !empty($user['mobile']) ? 'mobile' : 'email'; + $model->where($field, $user[$field]) + ->where('contact_type', 'client') + ->where('is_active', 1) + ->update(null, ['token_time_out' => $newExpiry]); + } else { + $model->update($id, ['token_time_out' => $newExpiry]); + } return true; } diff --git a/app/Helpers/JWTToken.php b/app/Helpers/JWTToken.php index c964bac7..f671197a 100755 --- a/app/Helpers/JWTToken.php +++ b/app/Helpers/JWTToken.php @@ -136,7 +136,7 @@ class JWTToken $token = $jwtParts[0]; try { - $decoded = JWT::decode($token, new Key("secret", 'HS512')); + $decoded = JWT::decode($token, new Key(env('JWT_SECRET'), 'HS512')); return json_encode(['status' => true, 'message' => 'Token is valid', 'data' =>$decoded->data->id ]); } catch (ExpiredException $e) { return json_encode(['status' => false, 'message' => 'Token has expired']); @@ -158,7 +158,7 @@ class JWTToken $jwtParts = explode(' ', $jwt); $token = $jwtParts[1]; - $decoded = JWT::decode($token, new Key("secret", 'HS512')); + $decoded = JWT::decode($token, new Key(env('JWT_SECRET'), 'HS512')); return $decoded->id; } @@ -169,7 +169,7 @@ class JWTToken $jwtParts = explode(' ', $jwt); $token = $jwtParts[1]; - $decoded = JWT::decode($token, new Key("secret", 'HS512')); + $decoded = JWT::decode($token, new Key(env('JWT_SECRET'), 'HS512')); return $decoded->role; } @@ -181,7 +181,7 @@ class JWTToken $jwtParts = explode(' ', $jwt); $token = $jwtParts[1]; - $decoded = JWT::decode($token, new Key("secret", 'HS512')); + $decoded = JWT::decode($token, new Key(env('JWT_SECRET'), 'HS512')); return $decoded; }