Guest User

Untitled

a guest
Dec 5th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import fire from '../fire';
  3. import { Link } from "react-router-dom";
  4. import ShowProfile from './showprofile';
  5.  
  6. export default class Login extends Component{
  7. constructor(props){
  8. super(props);
  9. this.state = {
  10. email : '',
  11. password : '',
  12. errorLogin : '',
  13. successLogin: false
  14. }
  15. this.login = this.login.bind(this);
  16. }
  17.  
  18. login = (event) => {
  19. event.preventDefault();
  20. const email = this.state.email;
  21. const password = this.state.password;
  22.  
  23. fire.auth()
  24. .signInWithEmailAndPassword(email, password)
  25. .then((result) => {
  26. var user = fire.auth().currentUser;
  27. if (user != null) {
  28. this.setState({successLogin: true})
  29. user.providerData.forEach(function (profile) {
  30. this.setState({email: profile.email})
  31. console.log(" Provider-specific UID: " + profile.uid);
  32. console.log(" Email: " + profile.email);
  33. });
  34. }
  35. })
  36. .catch((error) => {
  37. this.setState({errorLogin:error.message})
  38. });
  39. }
  40.  
  41. inputHandle = (event) => {
  42. const name = event.target.name;
  43. const value = event.target.value;
  44. this.setState({[name]: value, errorLogin: ''})
  45. }
  46.  
  47. render(){
  48. if(this.state.successLogin === true){
  49. return(
  50. <div className="container">
  51. <ShowProfile email={this.state.email}/>
  52. </div>
  53. )
  54. }
  55. return(
  56. <div className="container">
  57. <div className="col-md-5" style={{"margin": "auto"}}>
  58. <form onSubmit={this.login}>
  59. <h2 className="form-group">Login</h2>
  60. <h5 className="form-group text-danger" id="errorLogin">{ this.state.errorLogin }</h5>
  61. <div className="form-group">
  62. <input type="email"
  63. className="form-control"
  64. id="email"
  65. name="email"
  66. aria-describedby="emailHelp"
  67. placeholder="Enter email"
  68. onChange={(event) => { this.inputHandle(event) }}/>
  69. </div>
  70. <div className="form-group">
  71. <input type="password"
  72. className="form-control"
  73. id="password"
  74. name="password"
  75. placeholder="Password"
  76. onChange={(event) => { this.setState({password: event.target.value}) }}/>
  77. </div>
  78. <button type="submit" className="btn btn-primary">Submit</button>
  79. <hr/>
  80. <div className="form-group">
  81. <span>Not have user? Register <Link to="/register"> here </Link></span>
  82. </div>
  83. </form>
  84. </div>
  85. </div>
  86. )
  87. }
  88. }
Add Comment
Please, Sign In to add comment