Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. export const login = ({username, password}) => {
  2. return (dispatch) => {
  3. dispatch({type: LOGIN_START});
  4. fetch('https://***', {
  5. method: 'POST',
  6. headers: {
  7. 'Accept': 'application/json',
  8. 'Content-Type': 'application/json'
  9. },
  10. body: JSON.stringify({username: username, password: password})
  11. }).then((response) => {
  12. console.log(response);
  13. if (!response.ok) {
  14. throw Error("Error during login");
  15. }
  16. return response.json();
  17. }).then((responseJson) => {
  18. dispatch({
  19. type: LOGIN_SUCCESS,
  20. payload: {
  21. token: user.token
  22. }
  23. });
  24. }).catch((error) => {
  25. dispatch({type: LOGIN_FAIL, payload: error});
  26. });
  27. }
  28.  
  29. case LOGIN_SUCCESS:
  30. return {
  31. ...state,
  32. token: action.payload.token,
  33. loading: false
  34. }
  35.  
  36. componentWillReceiveProps(){
  37. if (this.props.token){
  38. Actions.main();
  39. }
  40. }
  41.  
  42. case LOGIN_SUCCESS:
  43. Actions.main();
  44. return {
  45. ...state,
  46. token: action.payload.token,
  47. loading: false
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement