-
Notifications
You must be signed in to change notification settings - Fork 0
/
practice.js
122 lines (95 loc) · 2.92 KB
/
practice.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
//superhero api
// useEffect(()=>{
// const options = {
// method: 'GET',
// url: 'https://superhero-search.p.rapidapi.com/api/heroes',
// headers: {
// 'X-RapidAPI-Key': 'f3fe02bcbamshbf696f35dc9fc92p1e4116jsn73c7dd0d0536',
// 'X-RapidAPI-Host': 'superhero-search.p.rapidapi.com'
// }
// };
// axios.request(options).then(function (response) {
// console.log(response.data);
// }).catch(function (error) {
// console.error(error);
// });
// axios.get("https://superhero-search.p.rapidapi.com/api/villains",{
// headers: {
// 'X-RapidAPI-Key': 'f3fe02bcbamshbf696f35dc9fc92p1e4116jsn73c7dd0d0536',
// 'X-RapidAPI-Host': 'superhero-search.p.rapidapi.com'
// }
// }).then((r)=>console.log("villan",r.data))
// },[])
//login api
// useEffect(()=>{
// fetch(
// "https://masai-api-mocker.herokuapp.com/auth/register",
// {
// method: "POST",
// body: form_data,
// mode: "no-cors",
// headers: {
// "Content-Type": "application/json",
// },
// }
// ).then((r)=>r.json()).then((r)=>console.log(r))
// },[])
let register = async (e) =>{
e.preventDefault();
let form_data ={
name:vl("name"),
email:vl("email"),
password:vl("password"),
username:vl("username"),
mobile:vl("mobile"),
description:vl("discription"),
}
console.log(form_data);
form_data = JSON.stringify(form_data);
let resp = await fetch("https://masai-api-mocker.herokuapp.com/auth/register",{
method:"POST",
body:form_data,
// mode:"no-cors",
headers:{
"Content-Type": "application/json",
},
});
let data = await resp.json();
console.log("data",data);
};
//login
let login = async () => {
let user_data = {
username: document.getElementById("username").value,
password: document.getElementById("password").value,
};
user_data = JSON.stringify(user_data);
let res = await fetch("https://masai-api-mocker.herokuapp.com/auth/login", {
method: "POST",
body: user_data,
// mode:"no-cors",
headers: {
"Content-Type": "application/json",
},
});
let data = await res.json();
let username = document.getElementById('username').value;
console.log(data);
getUserDetail(username, data.token);
};
document.getElementById("submit").addEventListener("click",login);
let getUserDetail = async (username, token) => {
console.log("here");
let res = await fetch(
`https://masai-api-mocker.herokuapp.com/user/${username}`,
{
method:"GET",
// mode:"no-cors",
headers: {
Authorization: `Bearer ${token}`,
},
}
);
let data = await res.json();
console.log("user data: ", data);
};