v0.7.19 - Implement automatic relay connection after login with authentication error handling
This commit is contained in:
84
api/index.js
84
api/index.js
@@ -83,6 +83,41 @@ function log(message, type = 'INFO') {
|
||||
// UI logging removed - using console only
|
||||
}
|
||||
|
||||
// Show authentication warning message
|
||||
function showAuthenticationWarning(message) {
|
||||
// Remove any existing warning
|
||||
hideAuthenticationWarning();
|
||||
|
||||
// Create warning element
|
||||
const warningDiv = document.createElement('div');
|
||||
warningDiv.id = 'auth-warning-message';
|
||||
warningDiv.className = 'auth-warning-message';
|
||||
warningDiv.innerHTML = `
|
||||
<div class="warning-content">
|
||||
<strong>⚠️ Authentication Issue:</strong> ${message}
|
||||
<br><br>
|
||||
<small>This usually means your pubkey is not authorized as an admin for this relay.
|
||||
Please check that you are using the correct admin pubkey that was shown during relay startup.</small>
|
||||
</div>
|
||||
`;
|
||||
|
||||
// Insert warning at the top of the relay connection section
|
||||
const relaySection = document.getElementById('relay-connection-section');
|
||||
if (relaySection) {
|
||||
relaySection.insertBefore(warningDiv, relaySection.firstChild);
|
||||
}
|
||||
|
||||
log(`Authentication warning displayed: ${message}`, 'WARNING');
|
||||
}
|
||||
|
||||
// Hide authentication warning message
|
||||
function hideAuthenticationWarning() {
|
||||
const warningDiv = document.getElementById('auth-warning-message');
|
||||
if (warningDiv) {
|
||||
warningDiv.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// NIP-59 helper: randomize created_at to thwart time-analysis (past 2 days)
|
||||
function randomNow() {
|
||||
const TWO_DAYS = 2 * 24 * 60 * 60; // 172800 seconds
|
||||
@@ -216,6 +251,9 @@ async function connectToRelay() {
|
||||
|
||||
log(`Connecting to relay: ${url}`, 'INFO');
|
||||
|
||||
// Clear any previous authentication warnings
|
||||
hideAuthenticationWarning();
|
||||
|
||||
let fetchedRelayInfo;
|
||||
|
||||
try {
|
||||
@@ -326,7 +364,18 @@ async function connectToRelay() {
|
||||
|
||||
} catch (error) {
|
||||
log(`Failed to connect to relay: ${error.message}`, 'ERROR');
|
||||
updateRelayConnectionStatus('error');
|
||||
|
||||
// Check if this is an authentication-related error
|
||||
if (error.message.includes('authentication') ||
|
||||
error.message.includes('auth') ||
|
||||
error.message.includes('permission') ||
|
||||
error.message.includes('unauthorized') ||
|
||||
error.message.includes('forbidden')) {
|
||||
updateRelayConnectionStatus('auth_error');
|
||||
showAuthenticationWarning(error.message);
|
||||
} else {
|
||||
updateRelayConnectionStatus('error');
|
||||
}
|
||||
|
||||
// Reset state on failure
|
||||
relayInfo = null;
|
||||
@@ -363,6 +412,9 @@ function disconnectFromRelay() {
|
||||
hideRelayInfo();
|
||||
updateAdminSectionsVisibility();
|
||||
|
||||
// Hide any authentication warnings
|
||||
hideAuthenticationWarning();
|
||||
|
||||
log('Disconnected from relay', 'INFO');
|
||||
|
||||
} catch (error) {
|
||||
@@ -403,6 +455,13 @@ function updateRelayConnectionStatus(status) {
|
||||
disconnectRelayBtn.disabled = true;
|
||||
restartRelayBtn.disabled = true;
|
||||
break;
|
||||
case 'auth_error':
|
||||
relayConnectionStatus.textContent = 'AUTHENTICATION FAILED';
|
||||
relayConnectionStatus.className = 'status error';
|
||||
connectRelayBtn.disabled = false;
|
||||
disconnectRelayBtn.disabled = true;
|
||||
restartRelayBtn.disabled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -569,9 +628,20 @@ function handleAuthEvent(event) {
|
||||
showProfileInHeader();
|
||||
loadUserProfile();
|
||||
|
||||
// Note: Configuration fetching now requires explicit relay connection
|
||||
// User must connect to relay manually after login
|
||||
console.log('Login successful. Connect to relay to access admin functions.');
|
||||
// Automatically attempt to connect to relay after successful login
|
||||
console.log('Login successful. Automatically attempting to connect to relay...');
|
||||
setTimeout(() => {
|
||||
connectToRelay().catch(error => {
|
||||
console.log(`Automatic relay connection failed: ${error.message}`);
|
||||
// Check if this is an authentication-related error
|
||||
if (error.message.includes('authentication') ||
|
||||
error.message.includes('auth') ||
|
||||
error.message.includes('permission') ||
|
||||
error.message.includes('unauthorized')) {
|
||||
showAuthenticationWarning(error.message);
|
||||
}
|
||||
});
|
||||
}, 500); // Small delay to allow profile loading to complete
|
||||
|
||||
} else if (error) {
|
||||
console.log(`Authentication error: ${error}`);
|
||||
@@ -589,6 +659,9 @@ function handleLogoutEvent() {
|
||||
// Clean up relay connection
|
||||
disconnectFromRelay();
|
||||
|
||||
// Hide any authentication warnings
|
||||
hideAuthenticationWarning();
|
||||
|
||||
// Reset UI - hide profile and show login modal
|
||||
hideProfileFromHeader();
|
||||
showLoginModal();
|
||||
@@ -882,6 +955,9 @@ async function logout() {
|
||||
isLoggedIn = false;
|
||||
currentConfig = null;
|
||||
|
||||
// Hide any authentication warnings
|
||||
hideAuthenticationWarning();
|
||||
|
||||
// Reset UI - hide profile and show login modal
|
||||
hideProfileFromHeader();
|
||||
// showLoginModal() removed - handled by handleLogoutEvent()
|
||||
|
||||
Reference in New Issue
Block a user