Spaces:
Running
Running
| // static/js/auth.js - COMPLETE AUTHENTICATION FUNCTIONS | |
| // Register | |
| async function register(event) { | |
| event.preventDefault(); | |
| const username = document.getElementById('username').value; | |
| const email = document.getElementById('email').value; | |
| const phone = document.getElementById('phone').value; | |
| const password = document.getElementById('password').value; | |
| const confirmPassword = document.getElementById('confirm_password').value; | |
| const fullName = document.getElementById('fullname').value; | |
| if (password !== confirmPassword) { | |
| showToast('Password tidak cocok!', 'error'); | |
| return; | |
| } | |
| if (password.length < 6) { | |
| showToast('Password minimal 6 karakter', 'error'); | |
| return; | |
| } | |
| const result = await apiCall('/api/auth/register', { | |
| method: 'POST', | |
| body: JSON.stringify({ | |
| username, email, phone, password, | |
| full_name: fullName | |
| }) | |
| }); | |
| if (result && result.success) { | |
| sessionStorage.setItem('pending_user_id', result.user_id); | |
| sessionStorage.setItem('verify_destination', result.destination); | |
| showToast('Registrasi berhasil! Kode: ' + result.verification_code, 'success'); | |
| window.location.href = '/verify?user_id=' + result.user_id; | |
| } | |
| } | |
| // Login | |
| async function login(event, method = 'email') { | |
| event.preventDefault(); | |
| let identifier, password; | |
| if (method === 'email') { | |
| identifier = document.getElementById('identifier').value; | |
| password = document.getElementById('password').value; | |
| } else { | |
| identifier = document.getElementById('phone').value; | |
| password = document.getElementById('phone-password').value; | |
| } | |
| if (!identifier || !password) { | |
| showToast('Isi semua field', 'error'); | |
| return; | |
| } | |
| const result = await apiCall('/api/auth/login', { | |
| method: 'POST', | |
| body: JSON.stringify({ identifier, password }) | |
| }); | |
| if (result && result.success) { | |
| showToast('Login berhasil!', 'success'); | |
| setTimeout(() => { | |
| window.location.href = '/dashboard'; | |
| }, 1000); | |
| } else if (result && result.requires_verification) { | |
| sessionStorage.setItem('pending_user_id', result.user_id); | |
| window.location.href = '/verify?user_id=' + result.user_id; | |
| } | |
| } | |
| // Logout | |
| async function logout() { | |
| if (confirm('Yakin ingin keluar?')) { | |
| await apiCall('/api/auth/logout', { method: 'POST' }); | |
| window.location.href = '/'; | |
| } | |
| } | |
| // Verify code | |
| async function verifyCode() { | |
| const digits = document.querySelectorAll('.code-digit'); | |
| let code = ''; | |
| digits.forEach(d => code += d.value); | |
| if (code.length !== 6) { | |
| showToast('Masukkan kode 6 digit', 'error'); | |
| return; | |
| } | |
| const userId = document.getElementById('userId')?.value || | |
| new URLSearchParams(window.location.search).get('user_id') || | |
| sessionStorage.getItem('pending_user_id'); | |
| const result = await apiCall('/api/auth/verify', { | |
| method: 'POST', | |
| body: JSON.stringify({ user_id: userId, code }) | |
| }); | |
| if (result && result.success) { | |
| showToast('Akun berhasil diverifikasi!', 'success'); | |
| sessionStorage.removeItem('pending_user_id'); | |
| sessionStorage.removeItem('verify_destination'); | |
| setTimeout(() => { | |
| window.location.href = '/login?verified=true'; | |
| }, 1500); | |
| } | |
| } | |
| // Resend code | |
| async function resendCode() { | |
| const userId = new URLSearchParams(window.location.search).get('user_id') || | |
| sessionStorage.getItem('pending_user_id'); | |
| const result = await apiCall('/api/auth/resend-code', { | |
| method: 'POST', | |
| body: JSON.stringify({ user_id: userId }) | |
| }); | |
| if (result && result.success) { | |
| showToast('Kode baru: ' + result.verification_code, 'success'); | |
| } | |
| } | |
| // Change password | |
| async function changePassword(event) { | |
| event.preventDefault(); | |
| const oldPassword = document.getElementById('oldPassword').value; | |
| const newPassword = document.getElementById('newPassword').value; | |
| const confirmPassword = document.getElementById('confirmPassword').value; | |
| if (!oldPassword || !newPassword || !confirmPassword) { | |
| showToast('Semua field harus diisi', 'error'); | |
| return; | |
| } | |
| if (newPassword !== confirmPassword) { | |
| showToast('Password baru tidak cocok', 'error'); | |
| return; | |
| } | |
| if (newPassword.length < 6) { | |
| showToast('Password minimal 6 karakter', 'error'); | |
| return; | |
| } | |
| const result = await apiCall('/api/auth/change-password', { | |
| method: 'POST', | |
| body: JSON.stringify({ | |
| old_password: oldPassword, | |
| new_password: newPassword | |
| }) | |
| }); | |
| if (result && result.success) { | |
| showToast('Password berhasil diubah', 'success'); | |
| setTimeout(() => { | |
| window.location.href = '/profile'; | |
| }, 1500); | |
| } | |
| } | |
| // Update profile | |
| async function updateProfile(field, value) { | |
| const data = {}; | |
| data[field] = value; | |
| const result = await apiCall('/api/auth/update-profile', { | |
| method: 'PUT', | |
| body: JSON.stringify(data) | |
| }); | |
| if (result && result.success) { | |
| showToast('Profil berhasil diperbarui', 'success'); | |
| loadProfile(); | |
| } | |
| } | |
| // Load profile | |
| async function loadProfile() { | |
| const result = await apiCall('/api/auth/me', { method: 'GET' }); | |
| if (result && !result.error) { | |
| const avatarInitial = document.getElementById('avatarInitial'); | |
| const fullNameDisplay = document.getElementById('fullNameDisplay'); | |
| const emailDisplay = document.getElementById('emailDisplay'); | |
| const usernameDisplay = document.getElementById('usernameDisplay'); | |
| const emailValue = document.getElementById('emailValue'); | |
| const phoneValue = document.getElementById('phoneValue'); | |
| const joinedDate = document.getElementById('joinedDate'); | |
| const verifiedBadge = document.getElementById('verifiedBadge'); | |
| if (avatarInitial) avatarInitial.textContent = (result.full_name || result.username).charAt(0).toUpperCase(); | |
| if (fullNameDisplay) fullNameDisplay.textContent = result.full_name || result.username; | |
| if (emailDisplay) emailDisplay.textContent = result.email || 'Belum diisi'; | |
| if (usernameDisplay) usernameDisplay.textContent = result.username; | |
| if (emailValue) emailValue.textContent = result.email || 'Belum diisi'; | |
| if (phoneValue) phoneValue.textContent = result.phone || 'Belum diisi'; | |
| if (joinedDate && result.created_at) { | |
| joinedDate.textContent = new Date(result.created_at).toLocaleDateString('id-ID'); | |
| } | |
| if (verifiedBadge && !result.is_verified) { | |
| verifiedBadge.innerHTML = '<i class="fas fa-clock"></i> Belum Verifikasi'; | |
| verifiedBadge.style.background = '#7f1a1a'; | |
| verifiedBadge.style.color = '#fecaca'; | |
| } | |
| } else if (result && result.error === 'Not logged in') { | |
| window.location.href = '/login'; | |
| } | |
| } | |
| // Load dashboard stats | |
| async function loadDashboardStats() { | |
| try { | |
| console.log("Loading stats..."); | |
| const result = await apiCall('/api/stats', { method: 'GET' }); | |
| console.log("Stats response:", result); | |
| if (result && !result.error) { | |
| const totalElement = document.getElementById('totalDetections'); | |
| const aiCountElement = document.getElementById('aiCount'); | |
| const humanCountElement = document.getElementById('humanCount'); | |
| if (totalElement) totalElement.textContent = result.total || 0; | |
| if (aiCountElement) aiCountElement.textContent = result.ai_count || 0; | |
| if (humanCountElement) humanCountElement.textContent = result.human_count || 0; | |
| } | |
| } catch (error) { | |
| console.error('Error loading stats:', error); | |
| } | |
| } | |
| // Load dashboard user profile | |
| async function loadDashboardUser() { | |
| const result = await apiCall('/api/auth/me', { method: 'GET' }); | |
| if (result && !result.error) { | |
| const profileName = document.getElementById('profileName'); | |
| const profileEmail = document.getElementById('profileEmail'); | |
| const avatarInitial = document.getElementById('avatarInitial'); | |
| if (profileName) profileName.textContent = result.full_name || result.username; | |
| if (profileEmail) profileEmail.textContent = result.email || result.phone || 'No contact'; | |
| if (avatarInitial) avatarInitial.textContent = (result.full_name || result.username).charAt(0).toUpperCase(); | |
| } else if (result && result.error === 'Not logged in') { | |
| window.location.href = '/login'; | |
| } | |
| } |