Login.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import Router from 'next/router';
  4. import { bindActionCreators } from 'redux';
  5. import { connect } from 'react-redux';
  6. import styled from 'styled-components';
  7. import emailValidator from 'email-validator';
  8. import LoginBox from './LoginBox';
  9. import LoginInputLabel from './LoginInputLabel';
  10. import TextInput from '../TextInput';
  11. import Button from '../Button';
  12. import Error from '../Error';
  13. import { loginUser, showAuthError, signupUser, showPageLoading } from '../../actions';
  14. import showRecaptcha from '../../helpers/recaptcha';
  15. import config from '../../config';
  16. const Wrapper = styled.div`
  17. flex: 0 0 auto;
  18. display: flex;
  19. align-items: center;
  20. margin: 24px 0 64px;
  21. `;
  22. const ButtonWrapper = styled.div`
  23. display: flex;
  24. justify-content: space-between;
  25. & > * {
  26. flex: 1 1 0;
  27. }
  28. & > *:last-child {
  29. margin-left: 32px;
  30. }
  31. @media only screen and (max-width: 768px) {
  32. & > *:last-child {
  33. margin-left: 16px;
  34. }
  35. }
  36. `;
  37. const VerificationMsg = styled.p`
  38. font-size: 24px;
  39. font-weight: 300;
  40. `;
  41. const User = styled.span`
  42. font-weight: normal;
  43. color: #512da8;
  44. border-bottom: 1px dotted #999;
  45. `;
  46. const ForgetPassLink = styled.a`
  47. align-self: flex-start;
  48. margin: -24px 0 32px;
  49. font-size: 14px;
  50. text-decoration: none;
  51. color: #2196f3;
  52. border-bottom: 1px dotted transparent;
  53. :hover {
  54. border-bottom-color: #2196f3;
  55. }
  56. `;
  57. const Recaptcha = styled.div`
  58. display: block;
  59. margin: 0 0 32px 0;
  60. `;
  61. class Login extends Component {
  62. constructor() {
  63. super();
  64. this.authHandler = this.authHandler.bind(this);
  65. this.loginHandler = this.loginHandler.bind(this);
  66. this.signupHandler = this.signupHandler.bind(this);
  67. this.goTo = this.goTo.bind(this);
  68. }
  69. componentDidMount() {
  70. showRecaptcha();
  71. }
  72. goTo(e) {
  73. e.preventDefault();
  74. const path = e.currentTarget.getAttribute('href');
  75. this.props.showPageLoading();
  76. Router.push(path);
  77. }
  78. authHandler(type) {
  79. const { loading, showError } = this.props;
  80. if (loading.login || loading.signup) return null;
  81. const form = document.getElementById('login-form');
  82. const { value: email } = form.elements.email;
  83. const { value: password } = form.elements.password;
  84. const { value: reCaptchaToken } = form.elements['g-recaptcha-input'];
  85. if (!email) return showError('Email address must not be empty.');
  86. if (!emailValidator.validate(email)) return showError('Email address is not valid.');
  87. if (password.trim().length < 8) {
  88. return showError('Password must be at least 8 chars long.');
  89. }
  90. if (!reCaptchaToken) {
  91. window.grecaptcha.reset();
  92. return showError('reCAPTCHA is not valid. Try again.');
  93. }
  94. window.grecaptcha.reset();
  95. return type === 'login'
  96. ? this.props.login({ email, password, reCaptchaToken })
  97. : this.props.signup({ email, password, reCaptchaToken });
  98. }
  99. loginHandler(e) {
  100. e.preventDefault();
  101. this.authHandler('login');
  102. }
  103. signupHandler(e) {
  104. e.preventDefault();
  105. this.authHandler('signup');
  106. }
  107. render() {
  108. return (
  109. <Wrapper>
  110. {this.props.auth.sentVerification ? (
  111. <VerificationMsg>
  112. A verification email has been sent to <User>{this.props.auth.user}</User>.
  113. </VerificationMsg>
  114. ) : (
  115. <LoginBox id="login-form" onSubmit={this.loginHandler}>
  116. <LoginInputLabel htmlFor="email" test="test">
  117. Email address
  118. </LoginInputLabel>
  119. <TextInput type="email" name="email" id="email" autoFocus />
  120. <LoginInputLabel htmlFor="password">Password (min chars: 8)</LoginInputLabel>
  121. <TextInput type="password" name="password" id="password" />
  122. <Recaptcha
  123. id="g-recaptcha"
  124. className="g-recaptcha"
  125. data-sitekey={config.RECAPTCHA_SITE_KEY}
  126. data-callback="recaptchaCallback"
  127. />
  128. <input type="hidden" id="g-recaptcha-input" name="g-recaptcha-input" />
  129. <ForgetPassLink href="/reset-password" title="Forget password" onClick={this.goTo}>
  130. Forgot your password?
  131. </ForgetPassLink>
  132. <ButtonWrapper>
  133. <Button
  134. icon={this.props.loading.login ? 'loader' : 'login'}
  135. onClick={this.loginHandler}
  136. big
  137. >
  138. Login
  139. </Button>
  140. <Button
  141. icon={this.props.loading.signup ? 'loader' : 'signup'}
  142. color="purple"
  143. onClick={this.signupHandler}
  144. big
  145. >
  146. Sign up
  147. </Button>
  148. </ButtonWrapper>
  149. <Error type="auth" left={0} />
  150. </LoginBox>
  151. )}
  152. </Wrapper>
  153. );
  154. }
  155. }
  156. Login.propTypes = {
  157. auth: PropTypes.shape({
  158. sentVerification: PropTypes.bool.isRequired,
  159. user: PropTypes.string.isRequired,
  160. }).isRequired,
  161. loading: PropTypes.shape({
  162. login: PropTypes.bool.isRequired,
  163. signup: PropTypes.bool.isRequired,
  164. }).isRequired,
  165. login: PropTypes.func.isRequired,
  166. signup: PropTypes.func.isRequired,
  167. showError: PropTypes.func.isRequired,
  168. showPageLoading: PropTypes.func.isRequired,
  169. };
  170. const mapStateToProps = ({ auth, loading }) => ({ auth, loading });
  171. const mapDispatchToProps = dispatch => ({
  172. login: bindActionCreators(loginUser, dispatch),
  173. signup: bindActionCreators(signupUser, dispatch),
  174. showError: bindActionCreators(showAuthError, dispatch),
  175. showPageLoading: bindActionCreators(showPageLoading, dispatch),
  176. });
  177. export default connect(mapStateToProps, mapDispatchToProps)(Login);