index.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { connect } from "react-redux";
  4. import { translate } from "react-i18next";
  5. import CustomInput from "components/CustomInput.jsx";
  6. import CustomErrors from "components/CustomMessages.jsx";
  7. import io from "io";
  8. @translate(["forgotPassword"], { wait: true })
  9. export default class ForgotPassword extends Component {
  10. static propTypes = {
  11. t: PropTypes.func,
  12. };
  13. static defaultProps = {
  14. t: () => {},
  15. };
  16. constructor(props) {
  17. super(props);
  18. CustomInput.initialize(this);
  19. this.state = {
  20. step: 1,
  21. resetCode: "",
  22. };
  23. }
  24. getActions = () => {
  25. const emailInput = <CustomInput key="email" type="email" name="email" label={ this.props.t("general:emailInput") } placeholder={ this.props.t("general:emailInput") } onRef={ ref => (this.input.email = ref) } />;
  26. const requestResetCodeButton = (<button key="requestResetCode" onClick={ this.requestResetCode }>
  27. { this.props.t("forgotPassword:requestResetCode") }
  28. </button>);
  29. const iAlreadyHaveAResetCodeButton = (<button key="skipRequestResetCode" onClick={ this.skipRequestResetCode }>
  30. { this.props.t("forgotPassword:iAlreadyHaveAResetCode") }
  31. </button>);
  32. const resetCodeInput = <CustomInput key="resetCode" type="uniqueCode" name="resetCode" label={ this.props.t("general:resetCodeInput") } placeholder={ this.props.t("general:resetCodeInput") } onRef={ ref => (this.input.resetCode = ref) } />;
  33. const verifyResetCode = (<button key="verifyResetCode" onClick={ this.verifyResetCode }>
  34. { this.props.t("forgotPassword:verifyResetCode") }
  35. </button>);
  36. const newPasswordInput = <CustomInput key="newPassword" type="password" name="newPassword" label={ this.props.t("general:newPasswordInput") } placeholder={ this.props.t("general:newPasswordInput") } onRef={ ref => (this.input.newPassword = ref) } />;
  37. const newPasswordAgainInput = <CustomInput key="newPasswordAgain" type="password" name="newPasswordAgain" label={ this.props.t("general:newPasswordAgainInput") } placeholder={ this.props.t("general:newPasswordAgainInput") } onRef={ ref => (this.input.newPasswordAgain = ref) } />;
  38. const changePassword = (<button key="changePassword" onClick={ this.changePassword }>
  39. { this.props.t("forgotPassword:changePassword") }
  40. </button>);
  41. if (this.state.step === 1) {
  42. return [emailInput, requestResetCodeButton, iAlreadyHaveAResetCodeButton];
  43. } else if (this.state.step === 2) {
  44. return [resetCodeInput, verifyResetCode];
  45. } return [newPasswordInput, newPasswordAgainInput, changePassword];
  46. };
  47. requestResetCode = () => {
  48. if (CustomInput.hasInvalidInput(this.input, ["email"])) {
  49. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  50. } else {
  51. this.messages.clearAll();
  52. io.getSocket(socket => {
  53. socket.emit("users.requestPasswordReset", this.input.email.getValue(), res => {
  54. if (res.status === "success") {
  55. this.messages.clearAddSuccess(this.props.t("forgotPassword:successfullyRequestedResetCode"));
  56. this.messages.clearAddInfo(this.props.t("forgotPassword:weHaveSentAUniqueResetCode"));
  57. this.setState({
  58. step: 2,
  59. });
  60. } else {
  61. this.messages.addError(res.message);
  62. }
  63. });
  64. });
  65. }
  66. };
  67. verifyResetCode = () => {
  68. if (CustomInput.hasInvalidInput(this.input, ["resetCode"])) {
  69. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  70. } else {
  71. this.messages.clearErrors();
  72. io.getSocket(socket => {
  73. socket.emit("users.verifyPasswordResetCode", this.input.resetCode.getValue(), res => {
  74. if (res.status === "success") {
  75. this.messages.clearAddSuccess(this.props.t("forgotPassword:successfullyVerifiedResetCode"));
  76. this.setState({
  77. step: 3,
  78. resetCode: this.input.resetCode.getValue(),
  79. });
  80. } else {
  81. this.messages.addError(res.message);
  82. }
  83. });
  84. });
  85. }
  86. };
  87. changePassword = () => {
  88. this.messages.clearErrorSuccess();
  89. if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) {
  90. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  91. } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) {
  92. this.messages.clearAddError(this.props.t("general:newPasswordNewPasswordAgainSameError"));
  93. } else {
  94. io.getSocket(socket => {
  95. socket.emit("users.changePasswordWithResetCode", this.state.resetCode, this.input.newPassword.getValue(), res => {
  96. if (res.status === "success") {
  97. this.messages.clearAddSuccess(this.props.t("forgotPassword:successfullyChangedPassword"));
  98. // TODO Maybe add 5s delay and replace location.href everywhere
  99. location.href = "/login";
  100. } else {
  101. this.messages.addError(res.message);
  102. }
  103. });
  104. });
  105. }
  106. };
  107. skipRequestResetCode = () => {
  108. this.setState({
  109. step: 2,
  110. });
  111. };
  112. render() {
  113. const { t } = this.props;
  114. return (
  115. <main id="forgotPassword">
  116. <h1>{ t("forgotPassword:title") }</h1>
  117. <div className="steps">
  118. <span className={ `step-circle ${ this.state.step === 1 ? "step-circle-active" : "" }` }>1</span>
  119. <span className="step-line" />
  120. <span className={ `step-circle ${ this.state.step === 2 ? "step-circle-active" : "" }` }>2</span>
  121. <span className="step-line" />
  122. <span className={ `step-circle ${ this.state.step === 3 ? "step-circle-active" : "" }` }>3</span>
  123. </div>
  124. <CustomErrors onRef={ ref => (this.messages = ref) } />
  125. { this.getActions() }
  126. </main>
  127. );
  128. }
  129. }