Last active
January 14, 2020 18:31
-
-
Save whiz25/a4ed629ca206552c1ea7c21ddeb6b1f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import axios from "axios"; | |
export default class Login extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
email: "", | |
password: "", | |
loginErrors: "" | |
}; | |
this.handleSubmit = this.handleSubmit.bind(this); | |
this.handleChange = this.handleChange.bind(this); | |
} | |
handleSubmit(event) { | |
const { email, password } = this.state; | |
axios | |
.post( | |
"http://localhost:3001/sessions", | |
{ | |
user: { | |
email: email, | |
password: password | |
} | |
}, | |
{ withCredentials: true } | |
) | |
.then(response => { | |
if (response.data.logged_in && response.data.patient) { | |
this.props.handleSuccessfulAuth(response.data); | |
} else { | |
this.props.handleSuccessfulDoctorAuth(response.data); | |
} | |
}) | |
.catch(error => { | |
console.log("login error", error); | |
}); | |
event.preventDefault(); | |
} | |
handleChange(event) { | |
this.setState({ | |
[event.target.name]: event.target.value | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<form onSubmit={this.handleSubmit}> | |
<div className="form-group"> | |
<input | |
className="form-control" | |
type="email" | |
name="email" | |
placeholder="Email" | |
required | |
value={this.state.email} | |
onChange={this.handleChange} | |
/> | |
</div> | |
<div className="form-group"> | |
<input | |
className="form-control" | |
type="password" | |
name="password" | |
placeholder="Password" | |
required | |
value={this.state.password} | |
onChange={this.handleChange} | |
/> | |
</div> | |
<button type="submit" className="btn btn-primary btn-sm"> | |
Login | |
</button> | |
</form> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment