Spaces:
Sleeping
Sleeping
Update src/templates/login.html
Browse files- src/templates/login.html +255 -209
src/templates/login.html
CHANGED
@@ -1,209 +1,255 @@
|
|
1 |
-
{% extends "auth_base.html" %}
|
2 |
-
|
3 |
-
{% block title %}ResearchMate - Login{% endblock %}
|
4 |
-
|
5 |
-
{% block content %}
|
6 |
-
<div class="row min-vh-100 justify-content-center align-items-center">
|
7 |
-
<div class="col-md-6 col-lg-4">
|
8 |
-
<div class="card shadow-lg">
|
9 |
-
<div class="card-body p-5">
|
10 |
-
<div class="text-center mb-4">
|
11 |
-
<i class="fas fa-user-lock fa-3x text-primary mb-3"></i>
|
12 |
-
<h3 class="text-primary-custom">Welcome to ResearchMate</h3>
|
13 |
-
<p class="text-muted">Please log in to access your research projects</p>
|
14 |
-
</div>
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
<div class="mb-3">
|
21 |
-
<label for="
|
22 |
-
<input type="
|
23 |
-
</div>
|
24 |
-
<div class="
|
25 |
-
<
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
</div>
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
<
|
56 |
-
|
57 |
-
<div class="mb-3">
|
58 |
-
<label for="reg-
|
59 |
-
<input type="
|
60 |
-
</div>
|
61 |
-
<div class="mb-3">
|
62 |
-
<label for="reg-
|
63 |
-
<input type="
|
64 |
-
</div>
|
65 |
-
<div class="mb-3">
|
66 |
-
<label for="reg-
|
67 |
-
<input type="password" class="form-control bg-white text-dark" id="reg-
|
68 |
-
</div>
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{% extends "auth_base.html" %}
|
2 |
+
|
3 |
+
{% block title %}ResearchMate - Login{% endblock %}
|
4 |
+
|
5 |
+
{% block content %}
|
6 |
+
<div class="row min-vh-100 justify-content-center align-items-center">
|
7 |
+
<div class="col-md-6 col-lg-4">
|
8 |
+
<div class="card shadow-lg">
|
9 |
+
<div class="card-body p-5">
|
10 |
+
<div class="text-center mb-4">
|
11 |
+
<i class="fas fa-user-lock fa-3x text-primary mb-3"></i>
|
12 |
+
<h3 class="text-primary-custom">Welcome to ResearchMate</h3>
|
13 |
+
<p class="text-muted">Please log in to access your research projects</p>
|
14 |
+
</div>
|
15 |
+
|
16 |
+
<!-- Alert container for messages -->
|
17 |
+
<div id="alert-container"></div>
|
18 |
+
|
19 |
+
<form id="login-form">
|
20 |
+
<div class="mb-3">
|
21 |
+
<label for="username" class="form-label">Username</label>
|
22 |
+
<input type="text" class="form-control" id="username" name="username" required>
|
23 |
+
</div>
|
24 |
+
<div class="mb-3">
|
25 |
+
<label for="password" class="form-label">Password</label>
|
26 |
+
<input type="password" class="form-control" id="password" name="password" required>
|
27 |
+
</div>
|
28 |
+
<div class="d-grid">
|
29 |
+
<button type="submit" class="btn btn-primary" id="login-btn">
|
30 |
+
<i class="fas fa-sign-in-alt me-2"></i>Login
|
31 |
+
</button>
|
32 |
+
</div>
|
33 |
+
</form>
|
34 |
+
<div class="text-center mt-4">
|
35 |
+
<p class="text-muted">
|
36 |
+
Don't have an account?
|
37 |
+
<a href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#registerModal">Register here</a>
|
38 |
+
</p>
|
39 |
+
</div>
|
40 |
+
</div>
|
41 |
+
</div>
|
42 |
+
</div>
|
43 |
+
</div>
|
44 |
+
|
45 |
+
<!-- Register Modal -->
|
46 |
+
<div class="modal fade" id="registerModal" tabindex="-1">
|
47 |
+
<div class="modal-dialog">
|
48 |
+
<div class="modal-content">
|
49 |
+
<div class="modal-header">
|
50 |
+
<h5 class="modal-title">
|
51 |
+
<i class="fas fa-user-plus me-2"></i><span style="color: #000;">Create Account</span>
|
52 |
+
</h5>
|
53 |
+
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
54 |
+
</div>
|
55 |
+
<div class="modal-body">
|
56 |
+
<form id="register-form">
|
57 |
+
<div class="mb-3">
|
58 |
+
<label for="reg-username" class="form-label">Username</label>
|
59 |
+
<input type="text" class="form-control bg-white text-dark" id="reg-username" name="username" required>
|
60 |
+
</div>
|
61 |
+
<div class="mb-3">
|
62 |
+
<label for="reg-email" class="form-label">Email</label>
|
63 |
+
<input type="email" class="form-control bg-white text-dark" id="reg-email" name="email" required>
|
64 |
+
</div>
|
65 |
+
<div class="mb-3">
|
66 |
+
<label for="reg-password" class="form-label">Password</label>
|
67 |
+
<input type="password" class="form-control bg-white text-dark" id="reg-password" name="password" required>
|
68 |
+
</div>
|
69 |
+
<div class="mb-3">
|
70 |
+
<label for="reg-confirm-password" class="form-label">Confirm Password</label>
|
71 |
+
<input type="password" class="form-control bg-white text-dark" id="reg-confirm-password" name="confirm_password" required>
|
72 |
+
</div>
|
73 |
+
</form>
|
74 |
+
</div>
|
75 |
+
<div class="modal-footer">
|
76 |
+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
77 |
+
<button type="submit" form="register-form" class="btn btn-primary">
|
78 |
+
<i class="fas fa-user-plus me-2"></i>Create Account
|
79 |
+
</button>
|
80 |
+
</div>
|
81 |
+
</div>
|
82 |
+
</div>
|
83 |
+
</div>
|
84 |
+
{% endblock %}
|
85 |
+
|
86 |
+
{% block extra_js %}
|
87 |
+
<script>
|
88 |
+
document.addEventListener('DOMContentLoaded', function() {
|
89 |
+
const loginForm = document.getElementById('login-form');
|
90 |
+
const registerForm = document.getElementById('register-form');
|
91 |
+
const loginBtn = document.getElementById('login-btn');
|
92 |
+
|
93 |
+
// Login handler
|
94 |
+
loginForm.addEventListener('submit', async function(e) {
|
95 |
+
e.preventDefault();
|
96 |
+
|
97 |
+
const username = document.getElementById('username').value.trim();
|
98 |
+
const password = document.getElementById('password').value;
|
99 |
+
|
100 |
+
if (!username || !password) {
|
101 |
+
showAlert('danger', 'Please fill in all fields');
|
102 |
+
return;
|
103 |
+
}
|
104 |
+
|
105 |
+
// Show loading state
|
106 |
+
const originalBtnText = loginBtn.innerHTML;
|
107 |
+
loginBtn.disabled = true;
|
108 |
+
loginBtn.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>Logging in...';
|
109 |
+
|
110 |
+
try {
|
111 |
+
const response = await fetch('/api/auth/login', {
|
112 |
+
method: 'POST',
|
113 |
+
headers: {
|
114 |
+
'Content-Type': 'application/json',
|
115 |
+
},
|
116 |
+
credentials: 'include', // Important: Include cookies
|
117 |
+
body: JSON.stringify({
|
118 |
+
username: username,
|
119 |
+
password: password
|
120 |
+
})
|
121 |
+
});
|
122 |
+
|
123 |
+
const data = await response.json();
|
124 |
+
|
125 |
+
if (response.ok && data.success) {
|
126 |
+
// Store token as backup
|
127 |
+
localStorage.setItem('authToken', data.token);
|
128 |
+
localStorage.setItem('userId', data.user_id);
|
129 |
+
localStorage.setItem('username', data.username);
|
130 |
+
|
131 |
+
// Show success message
|
132 |
+
showAlert('success', 'Login successful! Redirecting...');
|
133 |
+
|
134 |
+
// Redirect after short delay
|
135 |
+
setTimeout(() => {
|
136 |
+
window.location.href = data.redirect_url || '/';
|
137 |
+
}, 1500);
|
138 |
+
} else {
|
139 |
+
showAlert('danger', data.detail || data.error || 'Login failed. Please try again.');
|
140 |
+
}
|
141 |
+
} catch (error) {
|
142 |
+
console.error('Login error:', error);
|
143 |
+
showAlert('danger', 'Network error. Please check your connection and try again.');
|
144 |
+
} finally {
|
145 |
+
// Reset button state
|
146 |
+
loginBtn.disabled = false;
|
147 |
+
loginBtn.innerHTML = originalBtnText;
|
148 |
+
}
|
149 |
+
});
|
150 |
+
|
151 |
+
// Register handler
|
152 |
+
registerForm.addEventListener('submit', async function(e) {
|
153 |
+
e.preventDefault();
|
154 |
+
|
155 |
+
const username = document.getElementById('reg-username').value.trim();
|
156 |
+
const email = document.getElementById('reg-email').value.trim();
|
157 |
+
const password = document.getElementById('reg-password').value;
|
158 |
+
const confirmPassword = document.getElementById('reg-confirm-password').value;
|
159 |
+
|
160 |
+
if (!username || !email || !password || !confirmPassword) {
|
161 |
+
showAlert('danger', 'Please fill in all fields');
|
162 |
+
return;
|
163 |
+
}
|
164 |
+
|
165 |
+
if (password !== confirmPassword) {
|
166 |
+
showAlert('danger', 'Passwords do not match');
|
167 |
+
return;
|
168 |
+
}
|
169 |
+
|
170 |
+
if (password.length < 6) {
|
171 |
+
showAlert('danger', 'Password must be at least 6 characters long');
|
172 |
+
return;
|
173 |
+
}
|
174 |
+
|
175 |
+
try {
|
176 |
+
const response = await fetch('/api/auth/register', {
|
177 |
+
method: 'POST',
|
178 |
+
headers: {
|
179 |
+
'Content-Type': 'application/json',
|
180 |
+
},
|
181 |
+
body: JSON.stringify({
|
182 |
+
username: username,
|
183 |
+
email: email,
|
184 |
+
password: password
|
185 |
+
})
|
186 |
+
});
|
187 |
+
|
188 |
+
const data = await response.json();
|
189 |
+
|
190 |
+
if (response.ok && data.success) {
|
191 |
+
const modal = bootstrap.Modal.getInstance(document.getElementById('registerModal'));
|
192 |
+
modal.hide();
|
193 |
+
showAlert('success', 'Account created successfully! Please log in.');
|
194 |
+
registerForm.reset();
|
195 |
+
} else {
|
196 |
+
showAlert('danger', data.detail || data.error || 'Registration failed. Please try again.');
|
197 |
+
}
|
198 |
+
} catch (error) {
|
199 |
+
console.error('Registration error:', error);
|
200 |
+
showAlert('danger', 'Network error. Please check your connection and try again.');
|
201 |
+
}
|
202 |
+
});
|
203 |
+
|
204 |
+
function showAlert(type, message) {
|
205 |
+
const alertContainer = document.getElementById('alert-container');
|
206 |
+
|
207 |
+
// Clear existing alerts
|
208 |
+
alertContainer.innerHTML = '';
|
209 |
+
|
210 |
+
const alert = document.createElement('div');
|
211 |
+
alert.className = `alert alert-${type} alert-dismissible fade show`;
|
212 |
+
alert.innerHTML = `
|
213 |
+
<i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-triangle'} me-2"></i>
|
214 |
+
${message}
|
215 |
+
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
216 |
+
`;
|
217 |
+
|
218 |
+
alertContainer.appendChild(alert);
|
219 |
+
|
220 |
+
// Auto-remove after 5 seconds for error messages
|
221 |
+
if (type === 'danger') {
|
222 |
+
setTimeout(() => {
|
223 |
+
if (alert.parentNode) {
|
224 |
+
alert.remove();
|
225 |
+
}
|
226 |
+
}, 5000);
|
227 |
+
}
|
228 |
+
}
|
229 |
+
|
230 |
+
// Check if user is already logged in
|
231 |
+
const token = localStorage.getItem('authToken');
|
232 |
+
if (token) {
|
233 |
+
// Verify token is still valid
|
234 |
+
fetch('/api/user/status', {
|
235 |
+
headers: {
|
236 |
+
'Authorization': `Bearer ${token}`
|
237 |
+
},
|
238 |
+
credentials: 'include'
|
239 |
+
})
|
240 |
+
.then(response => {
|
241 |
+
if (response.ok) {
|
242 |
+
// User is already logged in, redirect
|
243 |
+
window.location.href = '/';
|
244 |
+
}
|
245 |
+
})
|
246 |
+
.catch(() => {
|
247 |
+
// Token is invalid, clear it
|
248 |
+
localStorage.removeItem('authToken');
|
249 |
+
localStorage.removeItem('userId');
|
250 |
+
localStorage.removeItem('username');
|
251 |
+
});
|
252 |
+
}
|
253 |
+
});
|
254 |
+
</script>
|
255 |
+
{% endblock %}
|