-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.js
138 lines (126 loc) · 3.4 KB
/
user.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
// getState is used to get the value of a state path
// setState is used to set the value of a state path
import { getState, setState } from "statezero";
import { setEmptyState } from "./helpers";
import { getUserCovers, defaultCover } from "./cover";
export const readCookie = () => {
const url = "/users/check-session";
fetch(url)
.then(res => {
if (res.status === 200) {
return res.json();
}
})
.then(json => {
if (json && json.currentUser) {
setState("currentUser", json.currentUser);
setState("userID", json.userID);
getUserCovers();
}
})
.catch(error => {
console.log(error);
});
};
export const updateLoginForm = field => {
const { name, value } = field;
setState(`loginForm.${name}`, value);
};
export const login = () => {
setState("loginClick", true);
// Create our request constructor with all the parameters we need
const request = new Request("/users/login", {
method: "post",
body: JSON.stringify(getState("loginForm")),
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
}
});
// Send the request with fetch()
fetch(request)
.then(res => {
if (res.status === 200) {
return res.json();
} else {
setState("failedLogin", true);
setTimeout(function() {
setState("failedLogin", false);
}, 3250);
setState("loginClick", false);
}
})
.then(json => {
if (json.currentUser !== undefined) {
setState("userID", json.userID);
setState("currentUser", json.currentUser);
getUserCovers();
setState("loginClick", false);
}
})
.catch(error => {
if (getState("failedLogin") !== true) {
setState("loginError", true);
setTimeout(function() {
setState("loginError", false);
}, 3250);
}
setState("loginClick", false);
console.log(error);
});
};
export const register = event => {
// Create our request constructor with all the parameters we need
const request = new Request("/users/register", {
method: "post",
body: JSON.stringify(getState("loginForm")),
headers: {
Accept: "application/json, text/plain, */*",
"Content-Type": "application/json"
}
});
// Send the request with fetch()
fetch(request)
.then(res => {
if (res.status === 200) {
return res.json();
}
})
.then(json => {
if (json !== undefined) {
// Successful registration
// Create the sample cover letter
defaultCover(json._id);
setState("registered", true);
setTimeout(function() {
setState("registered", false);
}, 3250);
login();
} else if (getState("loginForm").password.length < 6) {
// Short password
setState("passwordShort", true);
setTimeout(function() {
setState("passwordShort", false);
}, 3250);
} else {
// Invalid username
setState("invalidUsername", true);
setTimeout(function() {
setState("invalidUsername", false);
}, 3250);
}
})
.catch(error => {
console.log(error);
});
};
export const logout = () => {
const url = "/users/logout";
fetch(url)
.then(res => {
setEmptyState();
})
.catch(error => {
console.log(error);
});
};