Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. fetch(`${SERVER_URL}/api/signin`, {
  2. method: 'POST',
  3. mode: 'cors',
  4. body: JSON.stringify({username: email, password:password})})
  5. .then(response => {
  6. if (response.status !== 200) {
  7. debugger;
  8. const status = response.status;
  9. const statusText = response.statusText;
  10. throw("Could not login " + status + " " +statusText);
  11.  
  12. /*
  13. I would like to execute this in order to extract the data.msg from 401 errors
  14. but my errors get lost in the promise chain.
  15.  
  16. response.json()
  17. .then(data => { throw(data.msg)})
  18. .catch(() => {throw(status)}
  19. */
  20.  
  21. } else {
  22. return response.json()}
  23. })
  24. .then (data => {
  25. localStorage.setItem('token', data.access_token);
  26. dispatch({type: AUTH_USER});
  27. browserHistory.push('/welcome');
  28. })
  29. .catch(errorMsg => {
  30. dispatch(authError(errorMsg));
  31. })
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement