-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
159 lines (135 loc) · 4.03 KB
/
auth.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
let bv=localStorage.getItem("user")
// console.log(bv)
document.getElementById('home').addEventListener("click",op)
document.getElementById('logout').addEventListener("click",lo)
if(bv==null||bv=='n'){
document.querySelector("#change").innerHTML="Login / SignUp"
document.querySelector("#logout").innerHTML=""
}
else{
// console.log(1)
document.querySelector("#change").innerHTML=bv
document.querySelector("#logout").innerHTML="Logout"
}
function op(){
// window.location.assign("./home.html")
}
function lo(){
// console.log(1)
let e="n"
localStorage.setItem("user",e)
window.location.reload()
}
class User{
constructor(){
}
validateUsername(username){
// if (username.includes('@')){
// return false
// }
// else{
// return true
// }
return username.includes('@')?false:true
}
validatePassword(password){
return password.length>=8?true:false;
}
// https://github.com/masai-school/api-mocker/wiki/Authentication-API
// fetch causes the sign up to be async
async signup(n,e,u,p,m,d){
// console.log(1)
console.log(this.validateUsername("12@"))
console.log(this.validatePassword("12345678"))
let isValidated=this.validateUsername(u) && this.validatePassword(p);
if(isValidated){
console.log(1)
this.name=n;
this.email=e;
this.username=u;
this.password=p;
this.mobile=m;
this.description=d
const register_api='https://masai-api-mocker.herokuapp.com/auth/register'
const response=await fetch(register_api,{
method:'POST',
body: JSON.stringify(this),
headers:{
'Content-Type':'application/json'
}})
const data=await response.json()
console.log('data',data)
}
}
// rararararara
// 2121212121
async login(u,p){
try{
const login_data={
username:u,
password:p
};
// console.log(login_data)
const login_api="https://masai-api-mocker.herokuapp.com/auth/login"
const response=await fetch(login_api,{
method:'POST',
body:JSON.stringify(login_data),
headers:{
'Content-Type':'application/json'
}
})
const data=await response.json();
console.log(data)
return data
}
catch{
console.log("lag")
}
}
}
let users= new User()
const Register=()=>{
let reg_form=document.getElementById("reg_form")
const name=reg_form.name.value
const email=reg_form.email.value
const username=reg_form.username.value
const password=reg_form.password.value
const mobile=reg_form.mobile.value
const description=reg_form.description.value
users.signup(name,email,username,password,mobile,description)
// console.log(users.signup)
}
const Login=async()=>{
const username=document.getElementById('login-username').value
const password=document.getElementById('login-password').value
// console.log()
// let data = await users.login(username,password)
// using Destructuring
let {token} = await users.login(username,password)
getProfile(username,token)
}
const getProfile=async (username,token) => {
try{
let api_link=`https://masai-api-mocker.herokuapp.com/user/${username}`;
let response=await fetch(api_link,{
headers:{
Authorization:`Bearer ${token}`,
'Content-Type':'application/json'
}
})
let data=await response.json()
console.log(data)
if(data.message=='Invalid token for user'){
return 0
}
else{
localStorage.setItem("user",username)
window.location.reload()
}
}
catch(err){
console.log(err)
}
}
document.querySelector('#rg').addEventListener("click",Register)
document.querySelector("#lg").addEventListener("click",Login)