Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (187 loc) · 7.21 KB
/
Copy pathscript.js
File metadata and controls
219 lines (187 loc) · 7.21 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Global JWT decoder fallback
window.decodeJWT = function(token) {
try {
// Simple JWT decoder (for basic tokens)
const parts = token.split('.');
if (parts.length !== 3) {
throw new Error('Invalid JWT format');
}
// Decode the payload (second part)
const payload = parts[1];
// Add padding if needed
const paddedPayload = payload + '='.repeat((4 - payload.length % 4) % 4);
const decodedPayload = atob(paddedPayload.replace(/-/g, '+').replace(/_/g, '/'));
return JSON.parse(decodedPayload);
} catch (error) {
console.error('Error in fallback JWT decoder:', error);
throw error;
}
};
// Global JWT decode function that tries multiple methods
window.safeJWTDecode = function(token) {
try {
// Try to use the external library first
if (typeof jwtDecode !== 'undefined') {
return jwtDecode(token);
} else if (typeof jwt_decode !== 'undefined') {
// Fallback for older versions
return jwt_decode(token);
} else {
// Fallback to manual decoder
console.log('Using fallback JWT decoder');
return window.decodeJWT(token);
}
} catch (error) {
console.error('Error decoding JWT token:', error);
throw error;
}
};
// Configuration
const API_BASE_URL = 'http://localhost:8000'; // FastAPI default port
// Utility functions
function showAlert(message, type = 'error') {
const alertContainer = document.getElementById('alert-container');
const alert = document.createElement('div');
alert.className = `alert alert-${type}`;
alert.textContent = message;
alertContainer.innerHTML = '';
alertContainer.appendChild(alert);
setTimeout(() => {
alert.remove();
}, 5000);
}
function setLoading(button, isLoading) {
const btnText = button.querySelector('.btn-text');
if (isLoading) {
button.disabled = true;
btnText.innerHTML = '<span class="loading"></span>Processing...';
} else {
button.disabled = false;
btnText.textContent = button.id === 'loginBtn' ? 'Login' : 'Register';
}
}
function switchTab(tab) {
// Update tab buttons
document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active'));
event.target.classList.add('active');
// Update form containers
document.querySelectorAll('.form-container').forEach(container => container.classList.remove('active'));
document.getElementById(`${tab}-form`).classList.add('active');
// Clear alerts
document.getElementById('alert-container').innerHTML = '';
}
// Login form handler
document.getElementById('loginForm').addEventListener('submit', async (e) => {
e.preventDefault();
const loginBtn = document.getElementById('loginBtn');
const username = document.getElementById('loginUsername').value.trim();
const password = document.getElementById('loginPassword').value;
if (!username || !password) {
showAlert('Please fill in all fields');
return;
}
setLoading(loginBtn, true);
try {
const response = await fetch(`${API_BASE_URL}/api/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
const data = await response.json();
if (response.ok) {
localStorage.setItem('token', data.access_token);
localStorage.setItem('role', data.role);
localStorage.setItem('username', username);
showAlert('Login successful! Redirecting...', 'success');
// Redirect based on role
setTimeout(() => {
const role = encodeURIComponent(data.role.toLowerCase());
window.location.href = `${role}/${role}_dashboard.html`;
}, 1500);
} else {
showAlert(data.detail || 'Login failed. Please check your credentials.');
}
} catch (error) {
console.error('Login error:', error);
showAlert('Connection error. Please check if the server is running.');
} finally {
setLoading(loginBtn, false);
}
});
// Register form handler
document.getElementById('registerForm').addEventListener('submit', async (e) => {
e.preventDefault();
const registerBtn = document.getElementById('registerBtn');
const username = document.getElementById('registerUsername').value.trim();
const password = document.getElementById('registerPassword').value;
const role = document.getElementById('registerRole').value;
if (!username || !password || !role) {
showAlert('Please fill in all fields');
return;
}
if (password.length < 6) {
showAlert('Password must be at least 6 characters long');
return;
}
setLoading(registerBtn, true);
try {
const response = await fetch(`${API_BASE_URL}/api/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password, role }),
});
const data = await response.json();
if (response.ok) {
showAlert('Registration successful! Please login with your credentials.', 'success');
// Clear form and switch to login
document.getElementById('registerForm').reset();
setTimeout(() => {
switchTab('login');
document.getElementById('loginUsername').value = username;
}, 2000);
} else {
showAlert(data.detail || 'Registration failed. Please try again.');
}
} catch (error) {
console.error('Registration error:', error);
showAlert('Connection error. Please check if the server is running.');
} finally {
setLoading(registerBtn, false);
}
});
// Initialize page
document.addEventListener('DOMContentLoaded', () => {
// Check if user is already logged in
const token = localStorage.getItem('token');
const role = localStorage.getItem('role');
if (token && role) {
// Verify token is still valid by making a test request
fetch(`${API_BASE_URL}/api/jobs`, {
headers: {
'Authorization': `Bearer ${token}`
}
})
.then(response => {
if (response.ok) {
// Token is valid, redirect to dashboard
const page = encodeURIComponent(role.toLowerCase());
window.location.href = `${page}/${page}_dashboard.html`;
} else {
// Token is invalid, clear storage silently
localStorage.clear();
console.log('Invalid token cleared');
}
})
.catch((error) => {
// Connection error or other issues, clear storage silently
localStorage.clear();
console.log('Token cleared due to connection error:', error.message);
});
}
});
// Make switchTab globally available
window.switchTab = switchTab;