forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
135 lines (125 loc) · 4.09 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
// utils
import { logger } from 'utils/logs';
import { localAPI, controlTowerAPI } from 'utils/axios';
/**
* Logs in a user based on the email + password combination
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#login-email-password|here}
* @param {Object} options
* @returns {Object}
*/
export const loginUser = ({ email, password }) => {
logger.info('Login user');
return localAPI
.post('local-sign-in', { email, password })
.then(response => response.data);
};
/**
* This function sends a request to reset the user's password.
* It generates a token to be used in resetPassword
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#password-recovery|here}
* @param {Object} options
* @returns {Object}
*/
export const forgotPassword = ({ email }) => {
logger.info('Forgot password');
return controlTowerAPI
.post('auth/reset-password', { email }, { params: { origin: process.env.APPLICATIONS } })
.then(response => response.data)
.catch(({ response }) => {
const { status, statusText } = response;
if (status >= 300) {
logger.error(`Error requesting token for password reset: ${status}: ${statusText}`);
throw new Error(`Error requesting token for password reset: ${status}: ${statusText}`);
}
});
};
/**
* Register a new user based on the email + password combination
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#registration|here}
* @param {Object} options
* @returns {Object}
*/
export const registerUser = ({ email, password, repeatPassword }) => {
logger.info('Register user');
return controlTowerAPI
.post(
`auth/sign-up?origin=${process.env.APPLICATIONS}`,
{
email,
password,
repeatPassword,
apps: [process.env.APPLICATIONS]
}
)
.then(response => response.data)
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error registering user: ${status}: ${statusText}`);
throw new Error(`Error registering user: ${status}: ${statusText}`);
});
};
/**
* Resets the user's password.
* Needs the token hosted in the email sent in forgotPassword
* NOTE:this is NOT implemented in the API to be done from the app.
* right now the only way it's through the email link pointing to Control Tower.
* Check out the API docs for this endpoint {@link https://resource-watch.github.io/doc-api/index-rw.html#password-recovery|here}
* @param {Object} options
* @returns {Object}
*/
export const resetPassword = ({ tokenEmail, password, repeatPassword }) => {
logger.info('Reset password');
return controlTowerAPI
.post(
`auth/reset-password/${tokenEmail}?origin=${process.env.APPLICATIONS}`,
{ password, repeatPassword }
)
.then(response => response.data)
.catch(({ response }) => {
const { status, statusText } = response;
logger.error(`Error resetting user password: ${status}: ${statusText}`);
throw new Error(`Error resetting user password: ${status}: ${statusText}`);
});
};
/**
* Upload user photo
* @param {Blob} file file data
* @param {Object} user
*/
export const uploadPhoto = (file, user) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
const bodyObj = {
data: {
attributes: {
user_id: user.id,
avatar: reader.result
}
}
};
return fetch(`${process.env.WRI_API_URL}/profile`, {
method: 'POST',
body: JSON.stringify(bodyObj),
headers: {
'Content-Type': 'application/json',
Authorization: user.token
}
})
.then(response => response.json())
.then(({ data }) => {
resolve(data.attributes.avatar.original);
});
};
reader.onerror = (error) => {
reject(error);
};
});
export default {
loginUser,
forgotPassword,
registerUser,
resetPassword,
uploadPhoto
};