Login.jsx 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { Redirect } from 'react-router'
  4. import { translate } from 'react-i18next'
  5. import Cookies from 'js-cookie'
  6. import LoginLogo from '../component/Login/LoginLogo.jsx'
  7. import LoginLogoImg from '../img/logoTracimWhite.svg'
  8. import { postUserLogin } from '../action-creator.async.js'
  9. import Card from '../component/common/Card/Card.jsx'
  10. import CardHeader from '../component/common/Card/CardHeader.jsx'
  11. import CardBody from '../component/common/Card/CardBody.jsx'
  12. import InputGroupText from '../component/common/Input/InputGroupText.jsx'
  13. import Button from '../component/common/Input/Button.jsx'
  14. import LoginBtnForgotPw from '../component/Login/LoginBtnForgotPw.jsx'
  15. import {
  16. newFlashMessage,
  17. setUserConnected
  18. } from '../action-creator.sync.js'
  19. import { COOKIE, PAGE } from '../helper.js'
  20. import { Checkbox } from 'tracim_frontend_lib'
  21. class Login extends React.Component {
  22. constructor (props) {
  23. super(props)
  24. this.state = {
  25. inputLogin: {
  26. value: '',
  27. isInvalid: false
  28. },
  29. inputPassword: {
  30. value: '',
  31. isInvalid: false
  32. },
  33. inputRememberMe: false
  34. }
  35. }
  36. handleChangeLogin = e => this.setState({inputLogin: {...this.state.inputLogin, value: e.target.value}})
  37. handleChangePassword = e => this.setState({inputPassword: {...this.state.inputPassword, value: e.target.value}})
  38. handleChangeRememberMe = e => {
  39. e.preventDefault()
  40. e.stopPropagation()
  41. this.setState(prev => ({inputRememberMe: !prev.inputRememberMe}))
  42. }
  43. handleClickSubmit = async () => {
  44. const { history, dispatch, t } = this.props
  45. const { inputLogin, inputPassword, inputRememberMe } = this.state
  46. const fetchPostUserLogin = await dispatch(postUserLogin(inputLogin.value, inputPassword.value, inputRememberMe))
  47. const userAuth = btoa(`${inputLogin.value}:${inputPassword.value}`)
  48. if (fetchPostUserLogin.status === 200) {
  49. dispatch(setUserConnected({
  50. ...fetchPostUserLogin.json,
  51. auth: userAuth,
  52. logged: true
  53. }))
  54. if (inputRememberMe) {
  55. Cookies.set(COOKIE.USER_LOGIN, inputLogin.value, {expires: 365})
  56. Cookies.set(COOKIE.USER_AUTH, userAuth, {expires: 365})
  57. } else {
  58. Cookies.set(COOKIE.USER_LOGIN, inputLogin.value)
  59. Cookies.set(COOKIE.USER_AUTH, userAuth)
  60. }
  61. history.push(PAGE.WORKSPACE.ROOT)
  62. } else if (fetchPostUserLogin.status === 403) {
  63. dispatch(newFlashMessage(t('Email or password invalid'), 'danger'))
  64. }
  65. }
  66. render () {
  67. if (this.props.user.logged) return <Redirect to={{pathname: '/'}} />
  68. else {
  69. return (
  70. <section className='loginpage primaryColorBg'>
  71. <div className='container-fluid'>
  72. <LoginLogo customClass='loginpage__logo' logoSrc={LoginLogoImg} />
  73. <div className='row justify-content-center'>
  74. <div className='col-12 col-sm-11 col-md-8 col-lg-6 col-xl-4'>
  75. <Card customClass='loginpage__connection'>
  76. <CardHeader customClass='connection__header primaryColorBgLighten text-center'>{this.props.t('Connection')}</CardHeader>
  77. <CardBody formClass='connection__form'>
  78. <div>
  79. <InputGroupText
  80. parentClassName='connection__form__groupemail'
  81. customClass='mb-3 mt-4'
  82. icon='fa-envelope-open-o'
  83. type='email'
  84. placeHolder={this.props.t('Email Adress')}
  85. invalidMsg='Email invalide.'
  86. isInvalid={this.state.inputLogin.isInvalid}
  87. value={this.state.inputLogin.value}
  88. onChange={this.handleChangeLogin}
  89. />
  90. <InputGroupText
  91. parentClassName='connection__form__groupepw'
  92. customClass=''
  93. icon='fa-lock'
  94. type='password'
  95. placeHolder={this.props.t('Password')}
  96. invalidMsg='Mot de passe invalide.'
  97. isInvalid={this.state.inputPassword.isInvalid}
  98. value={this.state.inputPassword.value}
  99. onChange={this.handleChangePassword}
  100. />
  101. <div className='row align-items-center mt-4 mb-4'>
  102. <div className='col-12 col-sm-6 col-md-6 col-lg-6 col-xl-6'>
  103. <div className='connection__form__rememberme' onClick={this.handleChangeRememberMe}>
  104. <Checkbox
  105. name='inputRememberMe'
  106. checked={this.state.inputRememberMe}
  107. />
  108. Se souvenir de moi
  109. </div>
  110. <LoginBtnForgotPw
  111. customClass='connection__form__pwforgot'
  112. label={this.props.t('Forgotten password ?')}
  113. />
  114. </div>
  115. <div className='col-6 col-sm-6 col-md-6 col-lg-6 col-xl-6'>
  116. <Button
  117. htmlType='button'
  118. bootstrapType='primary'
  119. customClass='connection__form__btnsubmit ml-auto'
  120. label={this.props.t('Connection')}
  121. onClick={this.handleClickSubmit}
  122. />
  123. </div>
  124. </div>
  125. </div>
  126. </CardBody>
  127. </Card>
  128. </div>
  129. </div>
  130. </div>
  131. <footer className='loginpage__footer'>
  132. <div className='loginpage__footer__text d-flex align-items-center flex wrap'>
  133. copyright © 2013 - 2018 <a href='http://www.tracim.fr/' target='_blank' className='ml-3'>tracim.fr</a>
  134. </div>
  135. </footer>
  136. </section>
  137. )
  138. }
  139. }
  140. }
  141. const mapStateToProps = ({ user }) => ({ user })
  142. export default connect(mapStateToProps)(translate()(Login))