-
Notifications
You must be signed in to change notification settings - Fork 2
/
login.js
43 lines (41 loc) · 1.2 KB
/
login.js
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
let loginForm = document.getElementById("loginForm");
let apiUrl = "http://localhost:3000";
if(location.href.indexOf("netlify") >= 0 ){
apiUrl = "https://netflix-cp.herokuapp.com";
}
const queryString = location.search;
const urlParams = new URLSearchParams(queryString);
const existingEmail = urlParams.get('existingEmail');
const registered = urlParams.get('registered')
if(existingEmail){
loginForm.email.value = existingEmail
}
if(registered){
document.querySelector('.registered-alert').style.display = "block";
}
loginForm.addEventListener("submit", (e)=>{
e.preventDefault();
console.log(loginForm);
let payload = {
email: loginForm.email.value,
password: loginForm.password.value
}
fetch(apiUrl + "/login", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
.then((response)=>{
if (response.ok) {
return response.json();
} else {
throw new Error("something went wrong");
}
}) // returns a promise already
.then((response)=>{
localStorage.setItem('token', response.token)
location.href = "/";
})
})