index.jsx 5.4 KB

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