Update static/login.html
Browse files- static/login.html +35 -11
static/login.html
CHANGED
@@ -49,17 +49,21 @@
|
|
49 |
</div>
|
50 |
|
51 |
<script>
|
52 |
-
//
|
53 |
const API_BASE_URL = '';
|
54 |
|
55 |
const loginForm = document.getElementById('loginForm');
|
56 |
const signupBtn = document.getElementById('signupBtn');
|
|
|
57 |
const messageEl = document.getElementById('message');
|
58 |
|
|
|
59 |
loginForm.addEventListener('submit', async (e) => {
|
60 |
e.preventDefault();
|
61 |
messageEl.textContent = '';
|
|
|
62 |
|
|
|
63 |
const formData = new FormData();
|
64 |
formData.append('username', e.target.username.value);
|
65 |
formData.append('password', e.target.password.value);
|
@@ -71,35 +75,51 @@
|
|
71 |
});
|
72 |
|
73 |
if (response.ok) {
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
localStorage.setItem('accessToken', token);
|
76 |
-
|
77 |
-
// Send the token to the parent window (Settings.html)
|
78 |
if (window.parent) {
|
79 |
-
// The second argument is the target origin. '*' is
|
80 |
-
|
|
|
81 |
}
|
82 |
|
83 |
-
// Update UI to
|
84 |
messageEl.style.color = 'green';
|
85 |
-
messageEl.textContent = 'Login
|
|
|
86 |
} else {
|
87 |
const errorData = await response.json();
|
|
|
88 |
messageEl.textContent = errorData.detail || 'Login failed.';
|
89 |
}
|
90 |
} catch (error) {
|
91 |
console.error('Login error:', error);
|
92 |
-
messageEl.
|
|
|
|
|
|
|
93 |
}
|
94 |
});
|
95 |
|
|
|
|
|
96 |
signupBtn.addEventListener('click', async () => {
|
97 |
messageEl.textContent = '';
|
|
|
|
|
98 |
const username = loginForm.username.value;
|
99 |
const password = loginForm.password.value;
|
100 |
|
101 |
if (!username || !password) {
|
102 |
-
messageEl.
|
|
|
|
|
103 |
return;
|
104 |
}
|
105 |
|
@@ -115,11 +135,15 @@
|
|
115 |
messageEl.textContent = 'Signup successful! Please log in now.';
|
116 |
} else {
|
117 |
const errorData = await response.json();
|
|
|
118 |
messageEl.textContent = errorData.detail || 'Signup failed.';
|
119 |
}
|
120 |
} catch (error) {
|
121 |
console.error('Signup error:', error);
|
122 |
-
messageEl.
|
|
|
|
|
|
|
123 |
}
|
124 |
});
|
125 |
</script>
|
|
|
49 |
</div>
|
50 |
|
51 |
<script>
|
52 |
+
// Since this file is served from the same origin as the API, no base URL is needed.
|
53 |
const API_BASE_URL = '';
|
54 |
|
55 |
const loginForm = document.getElementById('loginForm');
|
56 |
const signupBtn = document.getElementById('signupBtn');
|
57 |
+
const loginBtn = document.getElementById('loginBtn');
|
58 |
const messageEl = document.getElementById('message');
|
59 |
|
60 |
+
// --- LOGIN LOGIC ---
|
61 |
loginForm.addEventListener('submit', async (e) => {
|
62 |
e.preventDefault();
|
63 |
messageEl.textContent = '';
|
64 |
+
loginBtn.disabled = true; // Prevent double-clicking
|
65 |
|
66 |
+
// FormData is used because the /token endpoint expects "form" data
|
67 |
const formData = new FormData();
|
68 |
formData.append('username', e.target.username.value);
|
69 |
formData.append('password', e.target.password.value);
|
|
|
75 |
});
|
76 |
|
77 |
if (response.ok) {
|
78 |
+
// *** THE FIX IS HERE: Parse the JSON response before using it ***
|
79 |
+
const data = await response.json();
|
80 |
+
const token = data.access_token;
|
81 |
+
|
82 |
+
// 1. Save token to this page's localStorage as a fallback.
|
83 |
localStorage.setItem('accessToken', token);
|
84 |
+
|
85 |
+
// 2. Send the token to the parent window (your local Settings.html)
|
86 |
if (window.parent) {
|
87 |
+
// The second argument is the target origin. '*' is required to allow
|
88 |
+
// communication with a local file (file://).
|
89 |
+
window.parent.postMessage({ accessToken: token }, '*');
|
90 |
}
|
91 |
|
92 |
+
// 3. Update UI to confirm success.
|
93 |
messageEl.style.color = 'green';
|
94 |
+
messageEl.textContent = 'Login Successful! You can return to the settings page.';
|
95 |
+
|
96 |
} else {
|
97 |
const errorData = await response.json();
|
98 |
+
messageEl.style.color = 'red';
|
99 |
messageEl.textContent = errorData.detail || 'Login failed.';
|
100 |
}
|
101 |
} catch (error) {
|
102 |
console.error('Login error:', error);
|
103 |
+
messageEl.style.color = 'red';
|
104 |
+
messageEl.textContent = 'An error occurred. Please try again.';
|
105 |
+
} finally {
|
106 |
+
loginBtn.disabled = false; // Re-enable the button
|
107 |
}
|
108 |
});
|
109 |
|
110 |
+
|
111 |
+
// --- SIGNUP LOGIC ---
|
112 |
signupBtn.addEventListener('click', async () => {
|
113 |
messageEl.textContent = '';
|
114 |
+
signupBtn.disabled = true;
|
115 |
+
|
116 |
const username = loginForm.username.value;
|
117 |
const password = loginForm.password.value;
|
118 |
|
119 |
if (!username || !password) {
|
120 |
+
messageEl.style.color = 'red';
|
121 |
+
messageEl.textContent = 'Please enter a username and password to sign up.';
|
122 |
+
signupBtn.disabled = false;
|
123 |
return;
|
124 |
}
|
125 |
|
|
|
135 |
messageEl.textContent = 'Signup successful! Please log in now.';
|
136 |
} else {
|
137 |
const errorData = await response.json();
|
138 |
+
messageEl.style.color = 'red';
|
139 |
messageEl.textContent = errorData.detail || 'Signup failed.';
|
140 |
}
|
141 |
} catch (error) {
|
142 |
console.error('Signup error:', error);
|
143 |
+
messageEl.style.color = 'red';
|
144 |
+
messageEl.textContent = 'An error occurred. Please try again.';
|
145 |
+
} finally {
|
146 |
+
signupBtn.disabled = false;
|
147 |
}
|
148 |
});
|
149 |
</script>
|