Perbaikan Generate Token Baru

This commit is contained in:
Ariyutta
2026-09-27 15:56:04 +07:00
parent 05ab6fee35
commit dcf57bcfdc
3 changed files with 40 additions and 18 deletions
+19 -5
View File
@@ -44,11 +44,15 @@ class VerifikasiAbsenLinkService
}
/**
* @return array{ok: bool, nip: ?string, reason: ?string}
* @param bool $renewSesi Saat true dan sesi sudah lewat 5 menit, token lama akan digantikan dengan
* token baru (bukan langsung ditolak). Dipakai saat tautan dibuka ulang dari
* WhatsApp (klik tombol "Tinjau Pengajuan"), supaya tidak perlu membuat
* pengajuan baru hanya untuk mendapatkan token baru.
* @return array{ok: bool, nip: ?string, reason: ?string, new_token: ?string}
*/
public function verify(int $pengajuanId, ?string $token): array
public function verify(int $pengajuanId, ?string $token, bool $renewSesi = false): array
{
$gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason];
$gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason, 'new_token' => null];
if (! $token) {
return $gagal('invalid');
@@ -67,13 +71,23 @@ class VerifikasiAbsenLinkService
return $gagal('link_expired');
}
$tokenBaru = null;
if ($row->first_accessed_at === null) {
$row->update(['first_accessed_at' => now()]);
} elseif (now()->greaterThan($row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT))) {
return $gagal('session_expired');
if (! $renewSesi) {
return $gagal('session_expired');
}
$tokenBaru = Str::random(40);
$row->update([
'token' => $this->hash($tokenBaru),
'first_accessed_at' => now(),
]);
}
return ['ok' => true, 'nip' => $row->nip, 'reason' => null];
return ['ok' => true, 'nip' => $row->nip, 'reason' => null, 'new_token' => $tokenBaru];
}
public function markUsed(int $pengajuanId, string $token): void