reset-password.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 {
  57. email: { value },
  58. } = form.elements;
  59. if (!value) {
  60. this.setState({ error: 'Please provide an Email address.' }, () => {
  61. setTimeout(() => {
  62. this.setState({ error: '' });
  63. }, 1500);
  64. });
  65. }
  66. this.setState({ loading: true });
  67. return axios
  68. .post('/api/auth/resetpassword', { email: value })
  69. .then(() =>
  70. this.setState({ success: 'Reset password email has been sent.', loading: false }, () => {
  71. setTimeout(() => {
  72. this.setState({ success: '' });
  73. }, 2500);
  74. })
  75. )
  76. .catch(() =>
  77. this.setState({ error: "Couldn't reset password", loading: false }, () => {
  78. setTimeout(() => {
  79. this.setState({ error: '' });
  80. }, 1500);
  81. })
  82. );
  83. }
  84. render() {
  85. const { error, loading, success } = this.state;
  86. return (
  87. <BodyWrapper>
  88. <Form id="reset-password-form" onSubmit={this.handleReset}>
  89. <TextInput type="email" name="email" id="email" placeholder="Email address" autoFocus />
  90. <Button onClick={this.handleReset} icon={loading ? 'loader' : ''} big>
  91. Reset password
  92. </Button>
  93. <Message error={!success && error}>
  94. {!success && error}
  95. {success}
  96. </Message>
  97. </Form>
  98. </BodyWrapper>
  99. );
  100. }
  101. }
  102. ResetPassword.getInitialProps = ({ reduxStore, query }) => {
  103. if (query && query.token) {
  104. reduxStore.dispatch(authUser(query.token));
  105. return { query };
  106. }
  107. return {};
  108. };
  109. ResetPassword.propTypes = {
  110. query: PropTypes.shape({
  111. token: PropTypes.string,
  112. }),
  113. };
  114. ResetPassword.defaultProps = {
  115. query: null,
  116. };
  117. export default ResetPassword;