Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const login = ({username, password}) => {
- return (dispatch) => {
- dispatch({type: LOGIN_START});
- fetch('https://***', {
- method: 'POST',
- headers: {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({username: username, password: password})
- }).then((response) => {
- console.log(response);
- if (!response.ok) {
- throw Error("Error during login");
- }
- return response.json();
- }).then((responseJson) => {
- dispatch({
- type: LOGIN_SUCCESS,
- payload: {
- token: user.token
- }
- });
- }).catch((error) => {
- dispatch({type: LOGIN_FAIL, payload: error});
- });
- }
- case LOGIN_SUCCESS:
- return {
- ...state,
- token: action.payload.token,
- loading: false
- }
- componentWillReceiveProps(){
- if (this.props.token){
- Actions.main();
- }
- }
- case LOGIN_SUCCESS:
- Actions.main();
- return {
- ...state,
- token: action.payload.token,
- loading: false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement