|
| 1 | +// MathCodeLab Certificate Verification Frontend |
| 2 | +(function() { |
| 3 | + const API_BASE = 'https://api.mathcodelab.de'; |
| 4 | + const mainContent = document.getElementById('main-content'); |
| 5 | + const loadingDiv = document.getElementById('loading'); |
| 6 | + const resultDiv = document.getElementById('result'); |
| 7 | + const searchForm = document.getElementById('search-form'); |
| 8 | + const yearSpan = document.getElementById('year'); |
| 9 | + if (yearSpan) yearSpan.textContent = new Date().getFullYear(); |
| 10 | + |
| 11 | + function getCertificateIdFromUrl() { |
| 12 | + // Support /verify/?id=... and /verify/ID |
| 13 | + const url = new URL(window.location.href); |
| 14 | + let id = url.searchParams.get('id'); |
| 15 | + if (!id) { |
| 16 | + // Try to extract from path |
| 17 | + const pathParts = url.pathname.split('/').filter(Boolean); |
| 18 | + const verifyIdx = pathParts.indexOf('verify'); |
| 19 | + if (verifyIdx !== -1 && pathParts.length > verifyIdx + 1) { |
| 20 | + id = pathParts[verifyIdx + 1]; |
| 21 | + } |
| 22 | + } |
| 23 | + return id; |
| 24 | + } |
| 25 | + |
| 26 | + function showLoading(show) { |
| 27 | + loadingDiv.classList.toggle('hidden', !show); |
| 28 | + resultDiv.innerHTML = ''; |
| 29 | + searchForm.classList.add('hidden'); |
| 30 | + } |
| 31 | + |
| 32 | + function showSearchForm() { |
| 33 | + searchForm.classList.remove('hidden'); |
| 34 | + loadingDiv.classList.add('hidden'); |
| 35 | + resultDiv.innerHTML = ''; |
| 36 | + } |
| 37 | + |
| 38 | + function showResult(html) { |
| 39 | + loadingDiv.classList.add('hidden'); |
| 40 | + searchForm.classList.add('hidden'); |
| 41 | + resultDiv.innerHTML = html; |
| 42 | + } |
| 43 | + |
| 44 | + function renderCertificate(data) { |
| 45 | + if (data.status === 'valid') { |
| 46 | + return ` |
| 47 | + <div class="status verified">Verified Certificate</div> |
| 48 | + <dl class="certificate-details"> |
| 49 | + <dt>Student Name</dt><dd>${escapeHtml(data.student_name)}</dd> |
| 50 | + <dt>Course Title</dt><dd>${escapeHtml(data.course_title)}</dd> |
| 51 | + <dt>Completion Date</dt><dd>${escapeHtml(data.completion_date)}</dd> |
| 52 | + <dt>Duration</dt><dd>${escapeHtml(data.duration_hours)} hours</dd> |
| 53 | + <dt>Issuer</dt><dd>${escapeHtml(data.issuer)}</dd> |
| 54 | + <dt>Instructor</dt><dd>${escapeHtml(data.instructor)}</dd> |
| 55 | + <dt>Certificate ID</dt><dd>${escapeHtml(data.certificate_id)}</dd> |
| 56 | + <dt>Verified At</dt><dd>${escapeHtml(data.verified_at)}</dd> |
| 57 | + </dl> |
| 58 | + <div class="note">This certificate was issued by MathCodeLab. For more information, visit <a href="https://mathcodelab.de" target="_blank">mathcodelab.de</a>.</div> |
| 59 | + `; |
| 60 | + } else if (data.status === 'revoked') { |
| 61 | + return ` |
| 62 | + <div class="status revoked">Certificate Revoked</div> |
| 63 | + <dl class="certificate-details"> |
| 64 | + <dt>Certificate ID</dt><dd>${escapeHtml(data.certificate_id)}</dd> |
| 65 | + ${data.revocation_reason ? `<dt>Revocation Reason</dt><dd>${escapeHtml(data.revocation_reason)}</dd>` : ''} |
| 66 | + </dl> |
| 67 | + <div class="note">This certificate was revoked by MathCodeLab.</div> |
| 68 | + `; |
| 69 | + } else { |
| 70 | + return ` |
| 71 | + <div class="status invalid">Certificate Not Found</div> |
| 72 | + <div class="certificate-details"> |
| 73 | + <p>No certificate with this ID was found in the MathCodeLab verification system.</p> |
| 74 | + </div> |
| 75 | + `; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + function escapeHtml(str) { |
| 80 | + return String(str).replace(/[&<>"']/g, function(tag) { |
| 81 | + const chars = { |
| 82 | + '&': '&', |
| 83 | + '<': '<', |
| 84 | + '>': '>', |
| 85 | + '"': '"', |
| 86 | + "'": ''' |
| 87 | + }; |
| 88 | + return chars[tag] || tag; |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + async function verifyCertificate(id) { |
| 93 | + showLoading(true); |
| 94 | + try { |
| 95 | + const resp = await fetch(`${API_BASE}/verify/${encodeURIComponent(id)}`); |
| 96 | + if (!resp.ok) throw new Error('not found'); |
| 97 | + const data = await resp.json(); |
| 98 | + showResult(renderCertificate(data)); |
| 99 | + } catch (err) { |
| 100 | + if (err.message === 'not found') { |
| 101 | + showResult(renderCertificate({status: 'invalid', certificate_id: id})); |
| 102 | + } else { |
| 103 | + showResult('<div class="status invalid">Error</div><div class="certificate-details"><p>Could not reach the verification server. Please try again later.</p></div>'); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Handle form submit |
| 109 | + searchForm.addEventListener('submit', function(e) { |
| 110 | + e.preventDefault(); |
| 111 | + const id = document.getElementById('certificate-id').value.trim(); |
| 112 | + if (id) verifyCertificate(id); |
| 113 | + }); |
| 114 | + |
| 115 | + // Main logic |
| 116 | + const certId = getCertificateIdFromUrl(); |
| 117 | + if (certId) { |
| 118 | + verifyCertificate(certId); |
| 119 | + } else { |
| 120 | + showSearchForm(); |
| 121 | + } |
| 122 | +})(); |
0 commit comments