-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairlock.ts
60 lines (52 loc) · 1.39 KB
/
airlock.ts
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
import { base } from '../airtable/airtable';
import { clearUserData, refreshUserData } from '../redux/userData';
const signupUser = async (username: string, password: string): Promise<{ success: boolean; message: string }> => {
let success, message;
try {
const res = await base.register({
username: username,
password: password,
});
success = res.body.success;
message = res.body.message;
} catch (err) {
success = false;
if (!username) {
message = 'Username required.';
} else if (!password) {
message = 'Password required.';
} else {
message = err.message;
}
}
return { success, message };
};
const loginUser = async (username: string, password: string): Promise<boolean> => {
try {
const res = await base.login({
username: username,
password: password,
});
refreshUserData(res.body.user);
return res.body.success;
} catch (err) {
console.log(err);
return false;
}
};
const logoutUser = async (): Promise<boolean> => {
try {
// We clear user data first so that users are logged out
// even if logout fails to reach backend in offline scenarios
clearUserData();
const res = await base.logout();
if (!res.body.success) {
return false;
}
return true;
} catch (err) {
console.log(err);
return false;
}
};
export { signupUser, loginUser, logoutUser };