设置登录
进一步完善日志
This commit is contained in:
@@ -72,6 +72,8 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
let s3Username = '';
|
||||
let s3Password = '';
|
||||
let s3AuthHeaders = {};
|
||||
const SAVED_AUTH_STORAGE_KEY = 'media-coding-web:s3-auth';
|
||||
const SAVED_BUCKET_STORAGE_KEY = 'media-coding-web:selected-bucket';
|
||||
|
||||
// Seek state
|
||||
let videoDuration = 0;
|
||||
@@ -768,6 +770,62 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
if (s3Password) s3AuthHeaders['X-S3-Password'] = s3Password;
|
||||
};
|
||||
|
||||
const saveAuthState = (username, password) => {
|
||||
try {
|
||||
window.localStorage.setItem(SAVED_AUTH_STORAGE_KEY, JSON.stringify({
|
||||
username,
|
||||
password
|
||||
}));
|
||||
} catch (error) {
|
||||
console.warn('Failed to persist auth state:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const loadAuthState = () => {
|
||||
try {
|
||||
const rawValue = window.localStorage.getItem(SAVED_AUTH_STORAGE_KEY);
|
||||
if (!rawValue) return null;
|
||||
const parsed = JSON.parse(rawValue);
|
||||
if (!parsed || typeof parsed.username !== 'string' || typeof parsed.password !== 'string') {
|
||||
return null;
|
||||
}
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
console.warn('Failed to read auth state:', error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const clearAuthState = () => {
|
||||
try {
|
||||
window.localStorage.removeItem(SAVED_AUTH_STORAGE_KEY);
|
||||
window.localStorage.removeItem(SAVED_BUCKET_STORAGE_KEY);
|
||||
} catch (error) {
|
||||
console.warn('Failed to clear auth state:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const saveSelectedBucket = (bucketName) => {
|
||||
try {
|
||||
if (bucketName) {
|
||||
window.localStorage.setItem(SAVED_BUCKET_STORAGE_KEY, bucketName);
|
||||
} else {
|
||||
window.localStorage.removeItem(SAVED_BUCKET_STORAGE_KEY);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to persist selected bucket:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const loadSelectedBucket = () => {
|
||||
try {
|
||||
return window.localStorage.getItem(SAVED_BUCKET_STORAGE_KEY) || '';
|
||||
} catch (error) {
|
||||
console.warn('Failed to read selected bucket:', error);
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
const showLogin = () => {
|
||||
if (loginScreen) loginScreen.classList.remove('hidden');
|
||||
if (appContainer) appContainer.classList.add('hidden');
|
||||
@@ -801,16 +859,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
loginError.classList.add('hidden');
|
||||
};
|
||||
|
||||
const login = async () => {
|
||||
if (!loginUsernameInput || !loginPasswordInput) return;
|
||||
const username = loginUsernameInput.value.trim();
|
||||
const password = loginPasswordInput.value;
|
||||
const login = async (options = {}) => {
|
||||
if (!loginUsernameInput || !loginPasswordInput) return false;
|
||||
const username = typeof options.username === 'string' ? options.username.trim() : loginUsernameInput.value.trim();
|
||||
const password = typeof options.password === 'string' ? options.password : loginPasswordInput.value;
|
||||
const skipErrorUi = options.skipErrorUi === true;
|
||||
if (!username || !password) {
|
||||
showLoginError('Please enter both access key and secret key.');
|
||||
return;
|
||||
if (!skipErrorUi) {
|
||||
showLoginError('Please enter both access key and secret key.');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
loginBtn.disabled = true;
|
||||
loginBtn.textContent = 'Logging in...';
|
||||
loginBtn.textContent = options.isRestoring ? 'Restoring...' : 'Logging in...';
|
||||
clearLoginError();
|
||||
|
||||
try {
|
||||
@@ -824,17 +885,31 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
if (!Array.isArray(data.buckets) || data.buckets.length === 0) {
|
||||
throw new Error('No buckets available for this account');
|
||||
}
|
||||
saveAuthState(username, password);
|
||||
if (loginUsernameInput) loginUsernameInput.value = username;
|
||||
if (loginPasswordInput) loginPasswordInput.value = password;
|
||||
renderBuckets(data.buckets);
|
||||
showApp();
|
||||
selectedBucket = data.buckets[0].Name;
|
||||
const savedBucket = loadSelectedBucket();
|
||||
selectedBucket = data.buckets.some((bucket) => bucket.Name === savedBucket)
|
||||
? savedBucket
|
||||
: data.buckets[0].Name;
|
||||
if (bucketSelect) bucketSelect.value = selectedBucket;
|
||||
saveSelectedBucket(selectedBucket);
|
||||
loadConfig();
|
||||
connectWebSocket();
|
||||
if (!ws) {
|
||||
connectWebSocket();
|
||||
}
|
||||
await fetchVideos(selectedBucket);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error('Login error:', err);
|
||||
showLoginError(err.message);
|
||||
clearAuthState();
|
||||
if (!skipErrorUi) {
|
||||
showLoginError(err.message);
|
||||
}
|
||||
setAuthHeaders('', '');
|
||||
return false;
|
||||
} finally {
|
||||
if (loginBtn) {
|
||||
loginBtn.disabled = false;
|
||||
@@ -843,6 +918,22 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
};
|
||||
|
||||
const restoreSavedSession = async () => {
|
||||
const savedAuth = loadAuthState();
|
||||
if (!savedAuth) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (loginUsernameInput) loginUsernameInput.value = savedAuth.username;
|
||||
if (loginPasswordInput) loginPasswordInput.value = savedAuth.password;
|
||||
return login({
|
||||
username: savedAuth.username,
|
||||
password: savedAuth.password,
|
||||
isRestoring: true,
|
||||
skipErrorUi: true
|
||||
});
|
||||
};
|
||||
|
||||
const clearDownloadCache = async () => {
|
||||
if (!clearDownloadCacheBtn) return;
|
||||
clearDownloadCacheBtn.disabled = true;
|
||||
@@ -1282,10 +1373,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
if (bucketSelect) {
|
||||
bucketSelect.addEventListener('change', async (event) => {
|
||||
selectedBucket = event.target.value;
|
||||
saveSelectedBucket(selectedBucket);
|
||||
await fetchVideos(selectedBucket);
|
||||
});
|
||||
}
|
||||
|
||||
// Initial state: require login before loading data
|
||||
showLogin();
|
||||
restoreSavedSession().then((restored) => {
|
||||
if (!restored) {
|
||||
showLogin();
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error('Session restore failed:', error);
|
||||
showLogin();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user