Skip to content

Instantly share code, notes, and snippets.

@a9kitkumar
Last active January 27, 2019 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a9kitkumar/389d1ae6c54df7b633901119b16a7f45 to your computer and use it in GitHub Desktop.
Save a9kitkumar/389d1ae6c54df7b633901119b16a7f45 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import authentication from './../services/authentication';
import { Link } from 'react-router-dom';
import './../App.css';
import './../CSS/login.css';
class login extends Component {
returnedData;
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
message: "",
isLoading: "",
};
};
//detects changes while admin input values
onChange = (e) => {
const state = this.state
state[e.target.name] = e.target.value;
this.setState(state);
}
// called when admin submit the form
onSubmit = (e) =>
{
e.preventDefault();
// this.state.isLoading = "Please wait..";
this.setState({isLoading: "Please wait.."});
authentication.login(this.state.email)
.then((result)=>{
// console.log(result.data.message)
console.log(result.data.objects[0].metadata.email)
if(result.data.message === "No objects returned.")
{
this.setState({message: "Wrong credentials, try again!!"});
this.setState({isLoading: ""});
this.timeout();
}
else
{
this.setState({message: ""});
this.setState({isLoading: ""});
localStorage.setItem('currentUser', result.data.objects[0].metadata.email)
this.props.history.push('/dashboard');
}
})
}
//timeout function
timeout()
{
setTimeout(
function() {
this.setState({message: ""});
}
.bind(this),
5000
);
}
render() {
return (
<div>
<div><Link to="/"><button type="submit" className="btn btn-success" >
Back
</button></Link> </div>
<div className="wrapper fadeInDown">
<div id="formContent">
<h2 className="active"><b> Admin Login </b></h2>
<form onSubmit={this.onSubmit}>
<input type="email" name="email" value={this.state.email} onChange={this.onChange} placeholder="Enter email" />
<input type="password" name="password" value={this.state.password} onChange={this.onChange} placeholder="Enter password" />
<input type="submit" className="fadeIn fourth" value="Login" />
<p>{this.state.isLoading}</p>
<p className="loginalert">{this.state.message}</p>
</form>
</div>
</div>
</div>
);
}
// called at the time of page load
componentDidMount() {
if(localStorage.getItem('currentUser'))
{
this.props.history.push('/dashboard');
}
}
}
export default login;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment