Advertisement
Guest User

Untitled

a guest
Oct 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. return fetch(Config.AUTH_URL, {
  2. method: 'POST',
  3. headers: {
  4. 'Accept': 'application/json',
  5. 'Content-Type': 'application/json',
  6. },
  7. body: JSON.stringify({
  8. email: email, //provided
  9. password: password //provided
  10. })
  11. })
  12. .then(response=> {
  13. if (response.ok) {
  14. return response.json().then(user=> {
  15. console.log(user);
  16. AsyncStorage.setItem('auth.token', user.api_token);
  17. AsyncStorage.setItem('user_name', user.user.name);
  18. AsyncStorage.setItem('user_email', user.user.email);
  19. })
  20. } else {
  21. if (response.status == 422) {
  22. //this needs to be rejected, but gives error
  23. return Response.reject(response.json());
  24.  
  25. } else {
  26. //when the response from server is unexpected error (eg. 500 without json data),I still want to reject with a json object
  27. return Promise(null, ()=> {
  28. return {"email": ["Technical problem, try later"]}
  29. });
  30. }
  31. }
  32. }, err=> {
  33. throw Error({"email": ["Technische fout. Probeer het opnieuw"]});
  34. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement