mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Add user authentication and dashboard features
- Implemented login and registration forms with validation. - Created dashboard layout to display user account information and pastes. - Added JavaScript functionality for fetching account data and pastes. - Styled authentication and dashboard pages with CSS. - Introduced modals for logout and account deletion confirmation. - Included error handling for user actions and API responses. ### Copilot generated commit message
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const registerForm = document.getElementById('register-form');
|
||||
const errorMessage = document.getElementById('error-message');
|
||||
|
||||
|
||||
checkAuthStatus();
|
||||
|
||||
registerForm.addEventListener('submit', async function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
|
||||
const username = document.getElementById('username').value.trim();
|
||||
const email = document.getElementById('email').value.trim();
|
||||
const password = document.getElementById('password').value;
|
||||
const confirmPassword = document.getElementById('confirm-password').value;
|
||||
|
||||
|
||||
if (!username || !password) {
|
||||
showError('Please fill in all required fields');
|
||||
return;
|
||||
}
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
showError('Passwords do not match');
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
showError('Password must be at least 6 characters long');
|
||||
return;
|
||||
}
|
||||
|
||||
if (username.length < 3) {
|
||||
showError('Username must be at least 3 characters long');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const submitButton = registerForm.querySelector('button[type="submit"]');
|
||||
const originalButtonText = submitButton.textContent;
|
||||
submitButton.disabled = true;
|
||||
submitButton.textContent = 'Creating account...';
|
||||
|
||||
|
||||
const requestData = {
|
||||
username: username,
|
||||
password: password
|
||||
};
|
||||
|
||||
|
||||
if (email) {
|
||||
requestData.email = email;
|
||||
}
|
||||
|
||||
|
||||
const response = await fetch('/api/accounts/register', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(requestData)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
window.location.href = '/dash';
|
||||
} else {
|
||||
showError(data.message || 'Registration failed. Please try again.');
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = originalButtonText;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error);
|
||||
showError('An unexpected error occurred. Please try again later.');
|
||||
|
||||
const submitButton = registerForm.querySelector('button[type="submit"]');
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = 'Register';
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function showError(message) {
|
||||
errorMessage.textContent = message;
|
||||
errorMessage.classList.add('visible');
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
errorMessage.classList.remove('visible');
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
|
||||
async function checkAuthStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/accounts/authorized');
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
window.location.href = '/dash';
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
console.log('Not logged in');
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user