Guest User

Untitled

a guest
Aug 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. render() {
  2.  
  3. if (this.state.redirectTo) {
  4. return (<Redirect to="/" />);
  5. } else {
  6. return ( ///rest of render here
  7.  
  8. constructor() {
  9. super();
  10. this.state = {
  11. username: '',
  12. password: '',
  13. redirectTo: false
  14. };
  15. this.handleSubmit = this.handleSubmit.bind(this);
  16. this.handleChange = this.handleChange.bind(this);
  17. }
  18.  
  19. handleChange(event) {
  20. this.setState({
  21. [event.target.name]: event.target.value
  22. });
  23. }
  24.  
  25.  
  26. componentDidMount() {
  27. this.setState({
  28. redirectTo: false
  29. });
  30. }
  31. handleSubmit(event) {
  32. event.preventDefault();
  33. console.log('handleSubmit');
  34.  
  35. axios
  36. .post('/user/login', {
  37. username: this.state.username,
  38. password: this.state.password
  39. })
  40. .then((response) => {
  41. console.log('login response: ');
  42. console.log(response);
  43. if (response.status === 200) {
  44. // update App.js state
  45. this.setState({
  46. redirectTo: true
  47. });
  48. this.props.updateUser({
  49. loggedIn: true,
  50. username: response.data.username,
  51. id: response.data.id
  52. });
  53. // update the state to redirect to home
  54. }
  55.  
  56. console.log(this.state.redirectTo);
  57. })
  58. .catch((error) => {
  59. console.log(this.state);
  60. console.log('login error: ');
  61. console.log(error);
  62. });
  63. }
Add Comment
Please, Sign In to add comment