-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
164 lines (156 loc) · 4.52 KB
/
app.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const selectForm = (ids) => {
const btns = document.querySelectorAll(".btn");
btns.forEach((btn) => {
btn.addEventListener("click", () => {
const login = document.querySelector(".login");
const signup = document.querySelector(".signup");
if (btn.id == ids[0]) {
btn.nextElementSibling.classList.remove("gray");
btn.nextElementSibling.classList.add("darkgray");
btn.classList.remove("darkgray");
btn.classList.add("gray");
signup.classList.add("hide");
login.classList.remove("hide");
} else {
btn.previousElementSibling.classList.remove("gray");
btn.previousElementSibling.classList.add("darkgray");
btn.classList.remove("darkgray");
btn.classList.add("gray");
signup.classList.remove("hide");
login.classList.add("hide");
}
});
});
};
const ids = ["login", "signup"];
selectForm(ids);
// headers
let headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append(
"X-RapidAPI-Key",
"8fd61c192emshb1fac7beb8b6214p14172bjsn2d3ae8bb6896"
);
headers.append("X-RapidAPI-Host", "demo-project75962.p.rapidapi.com");
// validate class
class ValidateAuth {
constructor(form, fields) {
this.form = form;
this.fields = fields;
this.requestClick();
}
requestClick = async () => {
let self = this;
self.form.addEventListener("submit", (e) => {
e.preventDefault();
const inputs = new Array();
let i = 0;
this.fields.forEach((field) => {
const input = document.querySelector(`#${field}`);
inputs[i++] = input.value.toString();
});
if (self.validateInputs(inputs)) {
console.log("true");
if (this.form.id === "login") {
this.loginAPI(inputs);
} else if (this.form.id === "signup") {
this.signupAPI(inputs);
}
}
});
};
loginAPI = async (inputs) => {
const url = "https://demo-project75962.p.rapidapi.com/auth/login";
const options = {
method: "POST",
headers: headers,
body: JSON.stringify({
username: `${inputs[0]}`,
password: `${inputs[1]}`,
}),
};
try {
const response = await fetch(url, options);
console.log(response.status);
const result = await response.json();
if (response.ok) {
localStorage.setItem("auth", 1);
localStorage.setItem("token", result.access_token);
this.form.submit();
} else {
const obj = new Alert();
obj.removeAlert();
obj.setAlert(result["message"]);
}
} catch (err) {
console.error(err);
}
};
signupAPI = async (inputs) => {
const url = "https://demo-project75962.p.rapidapi.com/auth/user";
const options = {
method: "POST",
headers: headers,
body: JSON.stringify({
username: `${inputs[0]}`,
email: `${inputs[1]}`,
password: `${inputs[2]}`,
}),
};
try {
const response = await fetch(url, options);
console.log(response.status);
const result = await response.json();
console.log(result);
if (response.ok) {
localStorage.setItem("auth", 1);
this.form.submit();
} else {
const obj = new Alert();
obj.removeAlert();
obj.setAlert(result["message"]);
}
} catch (err) {
console.error(err);
}
};
validateInputs = (inputs) => {
let str = inputs[0].trim();
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) === " ") {
this.boxAlert("username", true);
return false;
} else {
this.boxAlert("username", false);
}
}
str = inputs[inputs.length - 1].trim();
if (str.length < 8) {
this.boxAlert("password", true);
return false;
} else {
this.boxAlert("password", false);
}
return true;
};
boxAlert = (type, status) => {
let self = this;
let str = this.form.id + "-" + type;
const box = document.querySelector(`#${str}`);
if (status) {
box.style.borderBottom = "1px solid red";
} else {
box.style.borderBottom = "1px solid green";
}
};
}
const loginFields = ["login-username", "login-password"];
const signupFields = ["signup-username", "signup-email", "signup-password"];
const loginForm = document.querySelector(".login-form");
const signupForm = document.querySelector(".signup-form");
if (loginForm) {
const login = new ValidateAuth(loginForm, loginFields);
}
if (signupForm) {
const signup = new ValidateAuth(signupForm, signupFields);
}