Guest User

Untitled

a guest
Apr 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import React from 'react'
  2. import { css } from 'glamor'
  3. import { Auth } from 'aws-amplify'
  4.  
  5. import { withRouter } from 'react-router-dom'
  6.  
  7. class SignIn extends React.Component {
  8. state = {
  9. username: '',
  10. password: '',
  11. showConfirmation: false,
  12. user: {},
  13. authCode: ''
  14. }
  15. onChange = (key, value) => {
  16. this.setState({
  17. [key]: value
  18. })
  19. }
  20. signIn = () => {
  21. Auth.signIn(this.state.username, this.state.password)
  22. .then(user => {
  23. this.setState({ user, showConfirmation: true })
  24. })
  25. .catch(err => console.log('error signing in...: ', err))
  26. }
  27. confirmSignIn = () => {
  28. const { history } = this.props
  29. Auth.confirmSignIn(this.state.user, this.state.authCode)
  30. .then(user => {
  31. history.push('/')
  32. })
  33. .catch(err => console.log('error confirming signing in...: ', err))
  34. }
  35. render() {
  36. return (
  37. <div {...css(styles.container)}>
  38. {
  39. !this.state.showConfirmation && (
  40. <div {...css(styles.container)}>
  41. <input
  42. onChange={evt => this.onChange('username', evt.target.value)}
  43. {...css(styles.input)}
  44. placeholder='username'
  45. />
  46. <input
  47. type='password'
  48. onChange={evt => this.onChange('password', evt.target.value)}
  49. {...css(styles.input)}
  50. placeholder='password'
  51. />
  52. <div {...css(styles.button)} onClick={this.signIn}>
  53. <p {...css(styles.buttonText)}>Sign In</p>
  54. </div>
  55. </div>
  56. )
  57. }
  58. {
  59. this.state.showConfirmation && (
  60. <div>
  61. <input
  62. onChange={evt => this.onChange('authCode', evt.target.value)}
  63. {...css(styles.input)}
  64. placeholder='Confirmation Code'
  65. />
  66. <div {...css(styles.button)} onClick={this.confirmSignIn.bind(this)}>
  67. <p {...css(styles.buttonText)}>Confirm Sign In</p>
  68. </div>
  69. </div>
  70. )
  71. }
  72. </div>
  73. )
  74. }
  75. }
  76.  
  77. const styles = {
  78. button: {
  79. padding: '10px 60px',
  80. backgroundColor: '#ddd',
  81. cursor: 'pointer',
  82. borderRadius: '3px',
  83. ':hover': {
  84. backgroundColor: '#ededed'
  85. }
  86. },
  87. buttonText: {
  88. margin: 0
  89. },
  90. input: {
  91. height: 40,
  92. marginBottom: '10px',
  93. border: 'none',
  94. outline: 'none',
  95. borderBottom: '2px solid #4CAF50',
  96. fontSize: '16px',
  97. '::placeholder': {
  98. color: 'rgba(0, 0, 0, .3)'
  99. }
  100. },
  101. container: {
  102. flex: 1,
  103. paddingTop: '15px',
  104. display: 'flex',
  105. alignItems: 'center',
  106. justifyContent: 'center',
  107. flexDirection: 'column'
  108. }
  109. }
  110.  
  111. export default withRouter(SignIn)
Add Comment
Please, Sign In to add comment