tractatus/public/js/admin/login.js
TheFlow 4e4401a117 fix(auth): resolve admin login - token sanitization and missing password field
SUMMARY:
Fixed admin login failures caused by two issues:
1. Response sanitization middleware stripping auth tokens
2. Admin users missing password field in database

ROOT CAUSE ANALYSIS:
- sanitizeResponseData middleware removed ALL fields named 'token'
- This included authentication tokens that SHOULD be sent to clients
- Admin user records created without proper password field
- User.authenticate() failed on bcrypt.compare() with undefined password

FIXES:
1. Changed auth response field from 'token' to 'accessToken'
   - Avoids overly aggressive sanitization
   - More semantically correct (it's specifically an access token)
   - Frontend updated to use data.accessToken

2. Created fix-admin-user.js script
   - Properly creates admin user via User.create()
   - Ensures password field is bcrypt hashed
   - Deletes old malformed user records

3. Updated login.js auto-fill for correct dev email
   - Changed from admin@tractatus.local to admin@agenticgovernance.digital

TESTING:
- Local login now returns accessToken (308 char JWT)
- User object returned with proper ID serialization
- Auth flow: POST /api/auth/login → returns accessToken + user
- Ready for production deployment

FILES:
- src/controllers/auth.controller.js: Use accessToken field
- public/js/admin/login.js: Store data.accessToken, update default email
- scripts/fix-admin-user.js: Admin user creation/fix utility

NEXT STEPS:
1. Deploy to production
2. Run: node scripts/fix-admin-user.js admin@agenticgovernance.digital <password>
3. Test admin login at /admin/login.html

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-20 21:13:42 +13:00

59 lines
1.7 KiB
JavaScript

const loginForm = document.getElementById('login-form');
const errorMessage = document.getElementById('error-message');
const errorText = document.getElementById('error-text');
const loginBtn = document.getElementById('login-btn');
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
// Hide previous errors
errorMessage.classList.add('hidden');
// Disable button
loginBtn.disabled = true;
loginBtn.innerHTML = '<span>Signing in...</span>';
try {
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
const data = await response.json();
if (response.ok && data.success) {
// Store token
localStorage.setItem('admin_token', data.accessToken);
localStorage.setItem('admin_user', JSON.stringify(data.user));
// Redirect to dashboard
window.location.href = '/admin/dashboard.html';
} else {
// Show error
showError(data.message || 'Invalid credentials');
loginBtn.disabled = false;
loginBtn.innerHTML = 'Sign in';
}
} catch (error) {
console.error('Login error:', error);
showError('Network error. Please try again.');
loginBtn.disabled = false;
loginBtn.innerHTML = 'Sign in';
}
});
function showError(message) {
errorText.textContent = message;
errorMessage.classList.remove('hidden');
}
// Auto-fill for development (optional)
if (window.location.hostname === 'localhost') {
document.getElementById('email').value = 'admin@agenticgovernance.digital';
}