Guest User

Untitled

a guest
Feb 5th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. import { BrowserRouter as Router, Route, Link, Redirect } from "react-router-dom";
  2.  
  3. class Home extends Component {
  4.  
  5. constructor(props) {
  6. super(props)
  7. this.state = {
  8. dialogueOpen: false,
  9. loginMessage: '',
  10. name: '',
  11. password: '',
  12. newUsername: '',
  13. newPassword: '',
  14. returnedUser: undefined,
  15. registrationMessage: '',
  16. redirectToSignupSuccess: false
  17. }
  18. this.checkUniqueUsername = this.checkUniqueUsername.bind(this)
  19. this.handleChange = this.handleChange.bind(this)
  20. this.handleLogin = this.handleLogin.bind(this)
  21. this.handleOpen = this.handleOpen.bind(this)
  22. this.handleRegistration = this.handleRegistration.bind(this)
  23. }
  24.  
  25. handleRegistration = event => {
  26. //check that username and passwords aren't empty strings
  27. if (this.state.newUsername.length() > 0 && this.state.newPassword.length() > 0) {
  28. //check that the new username isn't already in the database
  29. this.checkUniqueUsername(this.state.newUsername)
  30. .then(isUnique => {
  31. console.log(isUnique)
  32.  
  33. if (isUnique === true) {
  34. //console.log("the username is unique")
  35. axios.post('http://localhost:4242/createuser', {
  36. username: this.state.newUsername,
  37. password: this.state.newPassword
  38. })
  39. .then( (response) => {
  40.  
  41. console.log(response)
  42. this.setState({redirectToSignupSuccess: true}, () => {
  43. console.log(this.state.redirectToSignupSuccess)
  44.  
  45. })
  46. //this.handleClose()
  47. })
  48. .catch((error) => {
  49. console.log(error);
  50. });
  51. }
  52. })
  53. }
  54. else {
  55. this.setState({registrationMessage:
  56. "Sorry, but we need a username and password for you to sign up"})
  57.  
  58. }
  59.  
  60. event.preventDefault();
  61. }
  62.  
  63. render() {
  64. let isAuthed = localStorage.getItem("authorized");
  65.  
  66. let redirectToSignupSuccess = this.state.redirectToSignupSuccess
  67.  
  68. if (isAuthed === "true") {
  69. return (<Redirect to='/inner' />)
  70. }
  71.  
  72. if (redirectToSignupSuccess === true) {
  73. return (<Redirect to='/signupsuccess' />)
  74. }
  75.  
  76. return (
  77. <the normal component view…>
Add Comment
Please, Sign In to add comment