service 4
This commit is contained in:
@@ -45,17 +45,46 @@ try {
|
||||
|
||||
unset($user['password']);
|
||||
|
||||
// توليد التوكن
|
||||
// توليد التوكن أو استرجاع التوكن الحالي إذا كان صالحاً
|
||||
$jwtService = new JwtService($redis);
|
||||
$role = 'service';
|
||||
$jwt = $jwtService->generateAccessToken($user['id'], $role, $audience);
|
||||
$ttl = 14400; // 4 hours
|
||||
$jwt = null;
|
||||
$expires_in = $ttl;
|
||||
|
||||
// محاولة استعادة التوكن الحالي من Redis لتجنب التكرار
|
||||
if ($redis) {
|
||||
$existingToken = $redis->get("active_token:{$user['id']}:{$audience}");
|
||||
if ($existingToken) {
|
||||
$decoded = $jwtService->decodeToken($existingToken);
|
||||
// يجب أن يكون التوكن صالحاً ويحتوي على بصمة الجهاز (إذا كان التشفير مفعلاً)
|
||||
if ($decoded && $decoded->exp > time() && (isset($decoded->fingerPrint) || empty($jwtService->getFpPepper()))) {
|
||||
$jwt = $existingToken;
|
||||
$expires_in = $decoded->exp - time();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// إذا لم يوجد توكن صالح، نولد واحداً جديداً ونلغي القديم
|
||||
if (!$jwt) {
|
||||
if ($redis) {
|
||||
$oldJti = $redis->get("active_jti:{$user['id']}");
|
||||
if ($oldJti) {
|
||||
$jwtService->revokeToken($oldJti, $ttl);
|
||||
}
|
||||
}
|
||||
$jwt = $jwtService->generateAccessToken($user['id'], $role, $audience, $fingerprint);
|
||||
$expires_in = $ttl;
|
||||
}
|
||||
|
||||
printSuccess([
|
||||
"message" => "Login successful",
|
||||
"data" => $user,
|
||||
"jwt" => $jwt,
|
||||
"expires_in" => 3600
|
||||
"expires_in" => $expires_in
|
||||
]);
|
||||
|
||||
} else {
|
||||
jsonError("Incorrect password");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user