diff --git a/src/api/user.js b/src/api/user.js index 0644858..9705dcc 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -1,9 +1,11 @@ import { config } from '../config'; +import { authHeader } from '../_helpers'; export const userAPI = { login, logout, + changePassword, }; function login(email, password) { @@ -30,18 +32,43 @@ function logout() { function handleResponse(response) { return response.text() .then(text => { - const data = text && JSON.parse(text); + const data = text && JSON.parse(text); - if (!response.ok) { - if (response.status === 401) { - logout(); - } - - const error = (data && data.message) || response.statusText; - return Promise.reject(error); + if (!response.ok) { + if (response.status === 401) { + logout(); } - return data; + const error = (data && data.message) || response.statusText; + return Promise.reject(error); } + + return data; + } ); -}; \ No newline at end of file +}; + + +function changePassword(user) { + const header = new Headers({ + 'Content-Type': 'application/json', + 'authorization': authHeader() + }); + + const options = { + method: 'POST', + headers: header, + body: JSON.stringify({ + newPassword: user.newPassword, + }) + }; + + return fetch(`${config.baseURL}users/changePassword`, options) + .then(res => { + if (res.ok) { + return res.json(); + } else { + throw new Error('Ops,problem'); + } + }); +} \ No newline at end of file