-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
68 lines (59 loc) · 2.19 KB
/
form.js
File metadata and controls
68 lines (59 loc) · 2.19 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { loginTalent } from "../services/services.js";
import { FORM_TEMPLATE } from "../templates/form.html.js";
import { HEADER, MAIN } from "../utils/elements.js"
import ProfileSection from "./aside.js";
import NavBar from "./nav.js";
import GraphSection from "./section.js";
export default class LoginForm extends HTMLElement {
constructor() {
super();
this.shadow = this.attachShadow({ mode: 'open' });
this.shadow.innerHTML = FORM_TEMPLATE;
this.form = this.shadow.querySelector('form');
this.alert = this.shadow.querySelector('p');
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'styles/components/form.css';
this.shadow.appendChild(link);
}
//__________________________________________________________
//
connectedCallback() {
this.#submission();
}
//_________________________________________________________________________
//
#submission() {
this.form.onsubmit = (event) => {
event.preventDefault()
const user = this.form.user.value;
const password = this.form.password.value;
this.form.user.value = '';
this.form.password.value = '';
const bytesTab = new TextEncoder().encode(`${user}:${password}`);
const credentials = btoa(String.fromCharCode(...bytesTab));
loginTalent(credentials)
.then(token => {
if (!token) {
throw new Error('Invalid Credentials')
};
localStorage.setItem('jwtToken', token);
this.remove();
HEADER.appendChild(new NavBar);
MAIN.append(
new ProfileSection,
new GraphSection
);
})
.catch((error) => {
this.alert.innerText = error;
});
}
}
//_________________________________________________________________________
//
static define(tag = 'login-form') {
customElements.define(tag, this)
}
}
LoginForm.define();