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
@@ -34,9 +34,9 @@ class VerifikasiAbsenController extends Controller
{ {
$pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id); $pengajuan = PengajuanPresensi::with(['pegawai', 'notes'])->findOrFail($id);
$otorisasi = $this->otorisasiAtasan($request, $pengajuan); $otorisasi = $this->otorisasiAtasan($request, $pengajuan, renewSesi: true);
if (is_array($otorisasi)) { if (! $otorisasi['ok']) {
return $this->responseTidakBerwenang($otorisasi['reason']); return $this->responseTidakBerwenang($otorisasi['reason']);
} }
@@ -44,6 +44,7 @@ class VerifikasiAbsenController extends Controller
'success' => true, 'success' => true,
'state' => $this->resolveState($pengajuan), 'state' => $this->resolveState($pengajuan),
'data' => $this->formatPengajuan($pengajuan), 'data' => $this->formatPengajuan($pengajuan),
'new_token' => $otorisasi['new_token'],
]); ]);
} }
@@ -58,11 +59,11 @@ class VerifikasiAbsenController extends Controller
$otorisasi = $this->otorisasiAtasan($request, $pengajuan); $otorisasi = $this->otorisasiAtasan($request, $pengajuan);
if (is_array($otorisasi)) { if (! $otorisasi['ok']) {
return $this->responseTidakBerwenang($otorisasi['reason']); return $this->responseTidakBerwenang($otorisasi['reason']);
} }
$nipAtasan = $otorisasi; $nipAtasan = $otorisasi['nip'];
if ($gagal = $this->cekBisaDiproses($pengajuan)) { if ($gagal = $this->cekBisaDiproses($pengajuan)) {
return $gagal; return $gagal;
@@ -117,11 +118,11 @@ class VerifikasiAbsenController extends Controller
$otorisasi = $this->otorisasiAtasan($request, $pengajuan); $otorisasi = $this->otorisasiAtasan($request, $pengajuan);
if (is_array($otorisasi)) { if (! $otorisasi['ok']) {
return $this->responseTidakBerwenang($otorisasi['reason']); return $this->responseTidakBerwenang($otorisasi['reason']);
} }
$nipAtasan = $otorisasi; $nipAtasan = $otorisasi['nip'];
if ($gagal = $this->cekBisaDiproses($pengajuan)) { if ($gagal = $this->cekBisaDiproses($pengajuan)) {
return $gagal; return $gagal;
@@ -201,23 +202,23 @@ class VerifikasiAbsenController extends Controller
} }
/** /**
* @return string|array{reason: string} Nip atasan kalau berhasil, atau array alasan gagal. * @return array{ok: bool, nip: ?string, reason: ?string, new_token: ?string}
*/ */
private function otorisasiAtasan(Request $request, PengajuanPresensi $pengajuan): string|array private function otorisasiAtasan(Request $request, PengajuanPresensi $pengajuan, bool $renewSesi = false): array
{ {
$token = $request->input('token'); $token = $request->input('token');
$hasil = $this->linkService->verify($pengajuan->id, $token); $hasil = $this->linkService->verify($pengajuan->id, $token, $renewSesi);
if (! $hasil['ok']) { if (! $hasil['ok']) {
return ['reason' => $hasil['reason']]; return ['ok' => false, 'nip' => null, 'reason' => $hasil['reason'], 'new_token' => null];
} }
if (! $this->dawaiService->isAtasanDari($hasil['nip'], $pengajuan->nip)) { if (! $this->dawaiService->isAtasanDari($hasil['nip'], $pengajuan->nip)) {
return ['reason' => 'invalid']; return ['ok' => false, 'nip' => null, 'reason' => 'invalid', 'new_token' => null];
} }
return $hasil['nip']; return ['ok' => true, 'nip' => $hasil['nip'], 'reason' => null, 'new_token' => $hasil['new_token']];
} }
private function responseTidakBerwenang(string $reason): JsonResponse private function responseTidakBerwenang(string $reason): JsonResponse
+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) { if (! $token) {
return $gagal('invalid'); return $gagal('invalid');
@@ -67,13 +71,23 @@ class VerifikasiAbsenLinkService
return $gagal('link_expired'); return $gagal('link_expired');
} }
$tokenBaru = null;
if ($row->first_accessed_at === null) { if ($row->first_accessed_at === null) {
$row->update(['first_accessed_at' => now()]); $row->update(['first_accessed_at' => now()]);
} elseif (now()->greaterThan($row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT))) { } 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 public function markUsed(int $pengajuanId, string $token): void
@@ -210,7 +210,7 @@
<script> <script>
const pengajuanId = {{ (int) $id }}; const pengajuanId = {{ (int) $id }};
const token = new URLSearchParams(window.location.search).get('token'); let token = new URLSearchParams(window.location.search).get('token');
const apiBase = "{{ url('/api/v1/verifikasi-absen') }}/" + pengajuanId; const apiBase = "{{ url('/api/v1/verifikasi-absen') }}/" + pengajuanId;
function showNotification(status, title, text) { function showNotification(status, title, text) {
@@ -288,6 +288,13 @@
url: `${apiBase}?token=${encodeURIComponent(token)}`, url: `${apiBase}?token=${encodeURIComponent(token)}`,
method: 'GET', method: 'GET',
success: function (res) { success: function (res) {
if (res.new_token) {
token = res.new_token;
const url = new URL(window.location.href);
url.searchParams.set('token', token);
window.history.replaceState({}, '', url);
}
tampilkanDetail(res.data, res.state); tampilkanDetail(res.data, res.state);
selesaiLoading(); selesaiLoading();
}, },