Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { BrowserRouter as Router, Route, Link, Redirect } from "react-router-dom";
- class Home extends Component {
- constructor(props) {
- super(props)
- this.state = {
- dialogueOpen: false,
- loginMessage: '',
- name: '',
- password: '',
- newUsername: '',
- newPassword: '',
- returnedUser: undefined,
- registrationMessage: '',
- redirectToSignupSuccess: false
- }
- this.checkUniqueUsername = this.checkUniqueUsername.bind(this)
- this.handleChange = this.handleChange.bind(this)
- this.handleLogin = this.handleLogin.bind(this)
- this.handleOpen = this.handleOpen.bind(this)
- this.handleRegistration = this.handleRegistration.bind(this)
- }
- handleRegistration = event => {
- //check that username and passwords aren't empty strings
- if (this.state.newUsername.length() > 0 && this.state.newPassword.length() > 0) {
- //check that the new username isn't already in the database
- this.checkUniqueUsername(this.state.newUsername)
- .then(isUnique => {
- console.log(isUnique)
- if (isUnique === true) {
- //console.log("the username is unique")
- axios.post('http://localhost:4242/createuser', {
- username: this.state.newUsername,
- password: this.state.newPassword
- })
- .then( (response) => {
- console.log(response)
- this.setState({redirectToSignupSuccess: true}, () => {
- console.log(this.state.redirectToSignupSuccess)
- })
- //this.handleClose()
- })
- .catch((error) => {
- console.log(error);
- });
- }
- })
- }
- else {
- this.setState({registrationMessage:
- "Sorry, but we need a username and password for you to sign up"})
- }
- event.preventDefault();
- }
- render() {
- let isAuthed = localStorage.getItem("authorized");
- let redirectToSignupSuccess = this.state.redirectToSignupSuccess
- if (isAuthed === "true") {
- return (<Redirect to='/inner' />)
- }
- if (redirectToSignupSuccess === true) {
- return (<Redirect to='/signupsuccess' />)
- }
- return (
- <the normal component view…>
Add Comment
Please, Sign In to add comment