Skip to content

Instantly share code, notes, and snippets.

@sankalpsingha
Last active March 27, 2017 07:57
Show Gist options
  • Save sankalpsingha/ab0e90091c67434c0fbbc69c185d1a51 to your computer and use it in GitHub Desktop.
Save sankalpsingha/ab0e90091c67434c0fbbc69c185d1a51 to your computer and use it in GitHub Desktop.
/*global fetch:false*/
export const emailChanged = (email) => {
return {
type: 'EMAIL_CHANGED',
payload: email
};
};
export const passwordChanged = (password) => {
return {
type: 'PASSWORD_CHANGED',
payload: password
};
};
export const loginUser = ({ email, password }) => {
return (dispatch) => {
dispatch({
type: 'LOAD_SPINNER'
});
fetch('http://localhost:3000/token', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
user: {
email,
password,
}
})
}).then((response) => {
console.log(response);
if (response.status === 401) {
console.log('AUTHENTICATION ERROR!!');
dispatch({
type: 'LOGIN_FAILED'
});
} else {
console.log('SUCCESS!!');
response.json().then(data => {
console.log(data);
dispatch({
type: 'LOGIN_USER_SUCCESS',
payload: data
});
});
}
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment