-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (102 loc) · 3.09 KB
/
index.js
File metadata and controls
118 lines (102 loc) · 3.09 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function login(event) {
event.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
};
fetch(`${CONFIG.basePath}/auth/login`, options)
.then((response) => {
if (!response.ok) throw new Error('Invalid credentials or server error');
return response.json();
})
.then((data) => {
sessionStorage.setItem('token', data.token);
sessionStorage.setItem('roles', JSON.stringify(data.roles));
const roles = data.roles;
// Check if user has any valid roles
const validRoles = [
'superAdmin',
'accountsAdmin',
'roomAdmin',
'cardAdmin',
'officeAdmin',
'foodAdmin',
'gateAdmin',
'adhyayanAdmin',
'travelAdmin',
'travelAdminDri',
'maintenanceAdmin',
'housekeepingAdmin',
'electricalAdmin',
'utsavAdmin',
'adhyayanAdminKol',
'adhyayanAdminRaj',
'adhyayanAdminDhu',
'avtAdmin',
'wifiAdmin',
'utsavAdminReadOnly',
'smilesAdmin',
'adhyayanAdminReadOnly',
'utsavAdminRaj'
];
const hasValidRole = roles.some((role) => validRoles.includes(role));
if (hasValidRole) {
// Always redirect to admin home for multi-role support
window.location.href = '/admin/adminhome.html';
} else {
// ❌ No recognized roles
alert(
'Login successful, but no valid role assigned. Please contact admin.'
);
}
})
.catch((error) => {
console.error('Login failed:', error);
alert('Login failed. Please check your credentials.');
});
}
function showResetModal() {
document.getElementById('resetModal').style.display = 'block';
}
function hideResetModal() {
document.getElementById('resetModal').style.display = 'none';
}
function resetPassword(event) {
event.preventDefault();
const username = document.getElementById('resetUsername').value.trim();
const newPassword = document.getElementById('newPassword').value;
if (!username) {
alert('Username is required to reset password.');
return;
}
if (!newPassword) {
alert('New password cannot be empty.');
return;
}
fetch(`${CONFIG.basePath}/auth/reset-password`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, newPassword })
})
.then(async (res) => {
const result = await res.json();
if (!res.ok) {
// Show specific backend error if available
throw new Error(result.message || 'Failed to reset password.');
}
alert(result.message || 'Password reset successful');
document.getElementById('resetPasswordForm').reset();
hideResetModal();
})
.catch((err) => {
console.error('Reset error:', err);
alert(err.message); // ✅ Show actual backend error
});
}