-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
72 lines (67 loc) · 1.58 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
module.exports = {
login(email, pass, cb) {
cb = arguments[arguments.length - 1]
if (localStorage.token) {
if (cb) cb(true)
this.onChange(true)
return
}
pretendRequest(email, pass, (res) => {
if (res.authenticated) {
localStorage.token = res.token
localStorage.currentUser = res._email
if (cb) cb(true)
this.onChange(true)
} else {
if (cb) cb(false)
this.onChange(false)
}
})
},
getToken() {
return localStorage.token
},
logout(cb) {
delete localStorage.token
delete localStorage.currentUser
if (cb) cb()
this.onChange(false)
},
loggedIn() {
return !!localStorage.token
},
onChange() {}
}
function pretendRequest(email, pass, cb) {
setTimeout(() => {
var abc = localStorage.getItem('credentials');
// debugger;
if(abc === null) {
cb({ authenticated: false })
return;
}
else if (abc) {
abc = JSON.parse(abc);
var authenticated = false;
for (var i = abc.length - 1; i >= 0; i--) {
if(abc[i].email === email && abc[i].password === pass) {
cb({
authenticated: true,
token: Math.random().toString(36).substring(7),
_email:email
})
authenticated = true;
}
};
if(!authenticated) {
// debugger;
cb({ authenticated: false })
}
console.log('loggedin');
}
else {
cb({ authenticated: false })
return;
}
}, 0)
}