Perbaikan API WA
This commit is contained in:
@@ -44,15 +44,11 @@ class VerifikasiAbsenLinkService
|
||||
}
|
||||
|
||||
/**
|
||||
* @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}
|
||||
* @return array{ok: bool, nip: ?string, reason: ?string}
|
||||
*/
|
||||
public function verify(int $pengajuanId, ?string $token, bool $renewSesi = false): array
|
||||
public function verify(int $pengajuanId, ?string $token): array
|
||||
{
|
||||
$gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason, 'new_token' => null];
|
||||
$gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason];
|
||||
|
||||
if (! $token) {
|
||||
return $gagal('invalid');
|
||||
@@ -71,23 +67,43 @@ 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))) {
|
||||
if (! $renewSesi) {
|
||||
return $gagal('session_expired');
|
||||
}
|
||||
|
||||
$tokenBaru = Str::random(40);
|
||||
$row->update([
|
||||
'token' => $this->hash($tokenBaru),
|
||||
'first_accessed_at' => now(),
|
||||
]);
|
||||
return $gagal('session_expired');
|
||||
}
|
||||
|
||||
return ['ok' => true, 'nip' => $row->nip, 'reason' => null, 'new_token' => $tokenBaru];
|
||||
return ['ok' => true, 'nip' => $row->nip, 'reason' => null];
|
||||
}
|
||||
|
||||
/**
|
||||
* Terbitkan token baru untuk pengajuan yang sesinya sudah habis, dipicu lewat aksi eksplisit
|
||||
* (tombol "Minta Token Baru" di halaman error), bukan otomatis saat tautan dibuka ulang.
|
||||
* Token lama tetap harus dikenali (belum invalid) dan link belum lewat masa berlaku (5 hari).
|
||||
*/
|
||||
public function regenerate(int $pengajuanId, ?string $token): ?string
|
||||
{
|
||||
if (! $token) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
||||
->where('token', $this->hash($token))
|
||||
->where('purpose', self::PURPOSE)
|
||||
->first();
|
||||
|
||||
if (! $row || now()->greaterThan($row->expires_at)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$tokenBaru = Str::random(40);
|
||||
|
||||
$row->update([
|
||||
'token' => $this->hash($tokenBaru),
|
||||
'first_accessed_at' => null,
|
||||
]);
|
||||
|
||||
return $tokenBaru;
|
||||
}
|
||||
|
||||
public function markUsed(int $pengajuanId, string $token): void
|
||||
|
||||
Reference in New Issue
Block a user