reset-password.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import Router from 'next/router';
  4. import styled, { css } from 'styled-components';
  5. import cookie from 'js-cookie';
  6. import axios from 'axios';
  7. import BodyWrapper from '../components/BodyWrapper';
  8. import TextInput from '../components/TextInput';
  9. import Button from '../components/Button';
  10. import { authUser } from '../actions';
  11. const Form = styled.form`
  12. position: relative;
  13. display: flex;
  14. flex-direction: column;
  15. align-items: flex-start;
  16. input {
  17. margin: 16px 0 32px;
  18. }
  19. `;
  20. const Message = styled.p`
  21. position: absolute;
  22. right: 0;
  23. bottom: 16px;
  24. font-size: 14px;
  25. color: green;
  26. ${({ error }) =>
  27. error &&
  28. css`
  29. color: red;
  30. `};
  31. @media only screen and (max-width: 768px) {
  32. bottom: 32px;
  33. font-size: 12px;
  34. }
  35. `;
  36. class ResetPassword extends Component {
  37. constructor() {
  38. super();
  39. this.state = {
  40. error: '',
  41. loading: false,
  42. success: '',
  43. };
  44. this.handleReset = this.handleReset.bind(this);
  45. }
  46. componentDidMount() {
  47. if (this.props.query || cookie.get('token')) {
  48. cookie.set('token', this.props.query.token, { expires: 7 });
  49. Router.push('/settings');
  50. }
  51. }
  52. handleReset(e) {
  53. if (this.state.loading) return null;
  54. e.preventDefault();
  55. const form = document.getElementById('reset-password-form');
  56. const { email: { value } } = form.elements;
  57. if (!value) {
  58. this.setState({ error: 'Please provide an Email address.' }, () => {
  59. setTimeout(() => {
  60. this.setState({ error: '' });
  61. }, 1500);
  62. });
  63. }
  64. this.setState({ loading: true });
  65. return axios
  66. .post('/api/auth/resetpassword', { email: value })
  67. .then(() =>
  68. this.setState({ success: 'Reset password email has been sent.', loading: false }, () => {
  69. setTimeout(() => {
  70. this.setState({ success: '' });
  71. }, 2500);
  72. })
  73. )
  74. .catch(() =>
  75. this.setState({ error: "Couldn't reset password", loading: false }, () => {
  76. setTimeout(() => {
  77. this.setState({ error: '' });
  78. }, 1500);
  79. })
  80. );
  81. }
  82. render() {
  83. const { error, loading, success } = this.state;
  84. return (
  85. <BodyWrapper>
  86. <Form id="reset-password-form" onSubmit={this.handleReset}>
  87. <TextInput type="email" name="email" id="email" placeholder="Email address" autoFocus />
  88. <Button onClick={this.handleReset} icon={loading ? 'loader' : ''} big>
  89. Reset password
  90. </Button>
  91. <Message error={!success && error}>
  92. {!success && error}
  93. {success}
  94. </Message>
  95. </Form>
  96. </BodyWrapper>
  97. );
  98. }
  99. }
  100. ResetPassword.getInitialProps = ({ reduxStore, query }) => {
  101. if (query && query.token) {
  102. reduxStore.dispatch(authUser(query.token));
  103. return { query };
  104. }
  105. return {};
  106. };
  107. ResetPassword.propTypes = {
  108. query: PropTypes.shape({
  109. token: PropTypes.string,
  110. }),
  111. };
  112. ResetPassword.defaultProps = {
  113. query: null,
  114. };
  115. export default ResetPassword;