TKK_E32230273/generate_pin_token.php

53 lines
1.6 KiB
PHP

<?php
include 'koneksi.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['status' => 'error', 'message' => 'Invalid method']);
exit;
}
$id_occupant = isset($_POST['id_occupant']) ? trim($_POST['id_occupant']) : '';
if (empty($id_occupant)) {
echo json_encode(['status' => 'error', 'message' => 'ID Occupant tidak ditemukan']);
exit;
}
try {
$token = bin2hex(random_bytes(16));
// Simpan token ke database (kolom token khusus verifikasi pin)
// Jika belum ada tabel, kita gunakan mekanisme JSON file untuk tokens sementara
$manifestPath = __DIR__ . '/manifest_pin_tokens.json';
$tokens = [];
if (file_exists($manifestPath)) {
$json = file_get_contents($manifestPath);
$tokens = json_decode($json, true) ?: [];
}
$tokens[$token] = [
'id_occupant' => $id_occupant,
'created_at' => time(),
'expires_at' => time() + 300 // Berlaku 5 menit
];
file_put_contents($manifestPath, json_encode($tokens, JSON_PRETTY_PRINT));
// Dapatkan URL dasar
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$qrUrl = $protocol . "://" . $host . $uri . "/verifikasi_wajah.php?token=" . urlencode($token);
echo json_encode([
'status' => 'success',
'token' => $token,
'url' => $qrUrl
]);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}