Skip to content

Instantly share code, notes, and snippets.

@MarcusHurney
Created March 2, 2016 14:07
Show Gist options
  • Save MarcusHurney/19ed2823d780e1aebaaf to your computer and use it in GitHub Desktop.
Save MarcusHurney/19ed2823d780e1aebaaf to your computer and use it in GitHub Desktop.
//This is from the login page submit handler, you can see I have success and error handlers
//Despite throwing an error, the page will still push onward to '/todos_index'
onSubmit(props) {
this.props.loginUser(props).then(() => {
this.context.router.push('/todos_index');
}, (response) => {
alert("Error cannot log in ", response);
});
}
//Here is the code that constitutes loginUser above, You'll see I have a console.log in the error block that does get called when run
//with a email and password that don't exist in the database
export function loginUser(props) {
const request = axios.post(`/users/login`, props);
request.then((response) => {
var token = response.headers.auth;
localStorage.setItem('token', token); //The user's token is taken from the reponse and set in local storage as 'token'
console.log('Token: ', localStorage.getItem('token'));
}, (response) => {
console.log("Error block in loginUser has been reached");
throw new Error(response.data.status);
});
return {
type: LOGIN_USER,
payload: request
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment