generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathlogin.mjs
More file actions
42 lines (36 loc) · 1.1 KB
/
login.mjs
File metadata and controls
42 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { apiService } from "../index.mjs";
/**
* Create a login component
* @param {string} template - The ID of the template to clone
* @param {Object} isLoggedIn - if you're logged in we don't need this component
* @returns {DocumentFragment} - The login fragment
*/
function createLogin(template, isLoggedIn) {
if (isLoggedIn) return;
const loginElement = document
.getElementById(template)
.content.cloneNode(true);
return loginElement;
}
// HANDLER
async function handleLogin(event) {
event.preventDefault();
const form = event.target;
const submitButton = form.querySelector("[data-submit]");
const originalText = submitButton.textContent;
try {
form.inert = true;
submitButton.textContent = "Logging in...";
const formData = new FormData(form);
const username = formData.get("username");
const password = formData.get("password");
await apiService.login(username, password);
} catch (error) {
throw error;
} finally {
// Always reset UI state regardless of success/failure
submitButton.textContent = originalText;
form.inert = false;
}
}
export { createLogin, handleLogin };