summaryrefslogtreecommitdiff
path: root/templates/js/auth.js
blob: 3ca7c4e00b00f125c6704d213a3874b41e83ca87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
document.addEventListener('DOMContentLoaded', () => {
    const changePasswordForm = document.getElementById('change-password-form');
    if (changePasswordForm) {
        changePasswordForm.addEventListener('submit', handleChangePassword);
    }
});

async function handleChangePassword(event) {
    event.preventDefault();
    const form = event.target;
    const formData = new FormData(form);
    const messagePlaceholder = document.getElementById('message-placeholder');
    messagePlaceholder.textContent = '';

    const newPassword = formData.get('new_password');
    const confirmNewPassword = formData.get('confirm_new_password');

    if (newPassword !== confirmNewPassword) {
        messagePlaceholder.className = 'error';
        messagePlaceholder.textContent = 'New passwords do not match.';
        return;
    }

    try {
        const response = await fetch(form.action, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: new URLSearchParams(formData)
        });

        const data = await response.json();

        if (!response.ok) {
            messagePlaceholder.className = 'error';
            messagePlaceholder.textContent = data.error || `Error: ${response.statusText}`;
        } else {
            messagePlaceholder.className = 'success';
            messagePlaceholder.textContent = data.message || 'Password updated successfully.';
        }

    } catch (error) {
        console.error('Password change failed:', error);
        messagePlaceholder.className = 'error';
        messagePlaceholder.textContent = 'An unexpected error occurred.';
    }
}