Update static/login.html
Browse files- static/login.html +75 -79
static/login.html
CHANGED
@@ -49,102 +49,98 @@
|
|
49 |
</div>
|
50 |
|
51 |
<script>
|
52 |
-
//
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
const loginBtn = document.getElementById('loginBtn');
|
58 |
-
const messageEl = document.getElementById('message');
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
-
//
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
body: formData
|
75 |
-
});
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
81 |
|
82 |
-
|
83 |
-
|
|
|
84 |
|
85 |
-
|
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 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
messageEl.style.color = 'red';
|
99 |
-
messageEl.textContent =
|
|
|
|
|
100 |
}
|
101 |
-
}
|
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 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
138 |
messageEl.style.color = 'red';
|
139 |
-
messageEl.textContent =
|
|
|
|
|
140 |
}
|
141 |
-
}
|
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>
|
150 |
</body>
|
|
|
49 |
</div>
|
50 |
|
51 |
<script>
|
52 |
+
// --- FIX: Wait for the page to load before trying to find elements ---
|
53 |
+
document.addEventListener('DOMContentLoaded', () => {
|
54 |
|
55 |
+
// Since this file is served from the same origin as the API, no base URL is needed.
|
56 |
+
const API_BASE_URL = '';
|
|
|
|
|
57 |
|
58 |
+
// --- FIX: Declarations are now INSIDE the listener ---
|
59 |
+
const loginForm = document.getElementById('loginForm');
|
60 |
+
const signupBtn = document.getElementById('signupBtn');
|
61 |
+
const loginBtn = document.getElementById('loginBtn');
|
62 |
+
const messageEl = document.getElementById('message');
|
63 |
|
64 |
+
// --- LOGIN LOGIC ---
|
65 |
+
loginForm.addEventListener('submit', async (e) => {
|
66 |
+
e.preventDefault();
|
67 |
+
messageEl.textContent = '';
|
68 |
+
loginBtn.disabled = true; // This will now work correctly
|
69 |
|
70 |
+
const formData = new FormData();
|
71 |
+
formData.append('username', e.target.username.value);
|
72 |
+
formData.append('password', e.target.password.value);
|
|
|
|
|
73 |
|
74 |
+
try {
|
75 |
+
const response = await fetch(`${API_BASE_URL}/token`, {
|
76 |
+
method: 'POST',
|
77 |
+
body: formData
|
78 |
+
});
|
79 |
|
80 |
+
if (response.ok) {
|
81 |
+
const data = await response.json();
|
82 |
+
const token = data.access_token;
|
83 |
|
84 |
+
localStorage.setItem('accessToken', token);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
if (window.parent) {
|
87 |
+
window.parent.postMessage({ accessToken: token }, '*');
|
88 |
+
}
|
89 |
+
|
90 |
+
messageEl.style.color = 'green';
|
91 |
+
messageEl.textContent = 'Login Successful! You can return to the settings page.';
|
92 |
+
} else {
|
93 |
+
const errorData = await response.json();
|
94 |
+
messageEl.style.color = 'red';
|
95 |
+
messageEl.textContent = errorData.detail || 'Login failed.';
|
96 |
+
}
|
97 |
+
} catch (error) {
|
98 |
+
console.error('Login error:', error);
|
99 |
messageEl.style.color = 'red';
|
100 |
+
messageEl.textContent = 'An error occurred. Please try again.';
|
101 |
+
} finally {
|
102 |
+
loginBtn.disabled = false; // This will also work correctly
|
103 |
}
|
104 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
// --- SIGNUP LOGIC ---
|
107 |
+
signupBtn.addEventListener('click', async () => {
|
108 |
+
messageEl.textContent = '';
|
109 |
+
signupBtn.disabled = true;
|
110 |
|
111 |
+
const username = loginForm.username.value;
|
112 |
+
const password = loginForm.password.value;
|
113 |
|
114 |
+
if (!username || !password) {
|
115 |
+
messageEl.style.color = 'red';
|
116 |
+
messageEl.textContent = 'Please enter a username and password to sign up.';
|
117 |
+
signupBtn.disabled = false;
|
118 |
+
return;
|
119 |
+
}
|
120 |
|
121 |
+
try {
|
122 |
+
const response = await fetch(`${API_BASE_URL}/signup`, {
|
123 |
+
method: 'POST',
|
124 |
+
headers: { 'Content-Type': 'application/json' },
|
125 |
+
body: JSON.stringify({ username, password })
|
126 |
+
});
|
127 |
|
128 |
+
if (response.status === 201) {
|
129 |
+
messageEl.style.color = 'green';
|
130 |
+
messageEl.textContent = 'Signup successful! Please log in now.';
|
131 |
+
} else {
|
132 |
+
const errorData = await response.json();
|
133 |
+
messageEl.style.color = 'red';
|
134 |
+
messageEl.textContent = errorData.detail || 'Signup failed.';
|
135 |
+
}
|
136 |
+
} catch (error) {
|
137 |
+
console.error('Signup error:', error);
|
138 |
messageEl.style.color = 'red';
|
139 |
+
messageEl.textContent = 'An error occurred. Please try again.';
|
140 |
+
} finally {
|
141 |
+
signupBtn.disabled = false;
|
142 |
}
|
143 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
});
|
145 |
</script>
|
146 |
</body>
|