reset-password.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import Router from 'next/router';
  4. import withRedux from 'next-redux-wrapper';
  5. import styled, { css } from 'styled-components';
  6. import cookie from 'js-cookie';
  7. import axios from 'axios';
  8. import initialState from '../store';
  9. import BodyWrapper from '../components/BodyWrapper';
  10. import TextInput from '../components/TextInput';
  11. import Button from '../components/Button';
  12. import { authUser } from '../actions';
  13. const Form = styled.form`
  14. position: relative;
  15. display: flex;
  16. flex-direction: column;
  17. align-items: flex-start;
  18. input {
  19. margin: 16px 0 32px;
  20. }
  21. `;
  22. const Message = styled.p`
  23. position: absolute;
  24. right: 0;
  25. bottom: 16px;
  26. font-size: 14px;
  27. color: green;
  28. ${({ error }) =>
  29. error &&
  30. css`
  31. color: red;
  32. `};
  33. @media only screen and (max-width: 768px) {
  34. bottom: 32px;
  35. font-size: 12px;
  36. }
  37. `;
  38. class ResetPassword extends Component {
  39. constructor() {
  40. super();
  41. this.state = {
  42. error: '',
  43. loading: false,
  44. success: '',
  45. };
  46. this.handleReset = this.handleReset.bind(this);
  47. }
  48. componentDidMount() {
  49. if (this.props.query || cookie.get('token')) {
  50. cookie.set('token', this.props.query.token, { expires: 7 });
  51. Router.push('/settings');
  52. }
  53. }
  54. handleReset(e) {
  55. if (this.state.loading) return null;
  56. e.preventDefault();
  57. const form = document.getElementById('reset-password-form');
  58. const { email: { value } } = 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 = ({ store, query }) => {
  103. if (query && query.token) {
  104. store.dispatch(authUser(query.token));
  105. return { query };
  106. }
  107. return null;
  108. };
  109. ResetPassword.propTypes = {
  110. query: PropTypes.shape({
  111. token: PropTypes.string,
  112. }),
  113. };
  114. ResetPassword.defaultProps = {
  115. query: null,
  116. };
  117. export default withRedux(initialState)(ResetPassword);