Guest User

Untitled

a guest
Dec 8th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { Auth } from 'aws-amplify';
  3.  
  4. class SignInForm extends Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = {
  9. user: '',
  10. password: '',
  11. signedIn: false
  12. };
  13. this.handleChange = this.handleChange.bind(this);
  14. this.handleSubmit = this.handleSubmit.bind(this);
  15. this.signIn = this.signIn.bind(this);
  16. this.confirmSignIn = this.confirmSignIn.bind(this);
  17. }
  18.  
  19. signIn() {
  20. const { username, password } = this.state;
  21. Auth.signIn({
  22. username: username,
  23. password: password
  24. })
  25. .then(() => console.log('successfully signed in'))
  26. .catch((err) => console.log(`Error signing in: ${ err }`))
  27. }
  28.  
  29. confirmSignIn() {
  30. const { username } = this.state;
  31. Auth.confirmSignIn(username)
  32. .then(() => console.log('successfully confirmed signed in'))
  33. .catch((err) => console.log(`Error confirming sign up - ${ err }`))
  34. }
  35.  
  36. handleSubmit(e) {
  37. e.preventDefault();
  38.  
  39. this.signIn();
  40. this.confirmSignIn()
  41. this.setState({
  42. username: '',
  43. password: '',
  44. signedIn: true
  45. });
  46. e.target.reset();
  47. }
  48.  
  49. handleChange(e) {
  50. if (e.target.id === 'username') {
  51. this.setState({
  52. username: e.target.value
  53. });
  54. } else if (e.target.id === 'password') {
  55. this.setState({
  56. password: e.target.value
  57. });
  58. }
  59. }
  60.  
  61. render() {
  62. const { signedIn } = this.state;
  63. if (signedIn) {
  64. return (
  65. <div>
  66. <h1>You have signed in!</h1>
  67. </div>
  68. );
  69. } else {
  70. return (
  71. <div>
  72. <form onSubmit={ this.handleSubmit }>
  73. <label>Username</label>
  74. <input id='username' type='text' onChange={ this.handleChange }/>
  75. <label>Password</label>
  76. <input id='password' type='password' onChange={ this.handleChange }/>
  77. <button>Sign In</button>
  78. </form>
  79. </div>
  80. );
  81. }
  82. }
  83. }
  84.  
  85. export default SignInForm;
Add Comment
Please, Sign In to add comment