ForgotPassword.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { connect } from "react-redux";
  4. import CustomInput from "./CustomInput.jsx";
  5. import CustomErrors from "./CustomErrors.jsx";
  6. import io from "../../io";
  7. @connect(state => ({
  8. user: {
  9. userId: state.user.get("userId"),
  10. },
  11. }))
  12. export default class Settings extends Component {
  13. static propTypes = {
  14. user: PropTypes.object,
  15. };
  16. static defaultProps = {
  17. user: {
  18. userId: "",
  19. },
  20. };
  21. constructor(props) {
  22. super(props);
  23. CustomInput.initialize(this);
  24. this.state = {
  25. step: 1,
  26. resetCode: "",
  27. };
  28. }
  29. getActions = () => {
  30. const emailInput = <CustomInput key="email" type="email" name="email" label="Email" placeholder="Email" onRef={ ref => (this.input.email = ref) } />;
  31. const requestResetCodeButton = (<button key="requestResetCode" onClick={ this.requestResetCode }>
  32. Request reset code
  33. </button>);
  34. const iAlreadyHaveAResetCodeButton = (<button key="skipRequestResetCode" onClick={ this.skipRequestResetCode }>
  35. I already have a reset code
  36. </button>);
  37. const resetCodeInput = <CustomInput key="resetCode" type="uniqueCode" name="resetCode" label="Reset code" placeholder="Reset code" onRef={ ref => (this.input.email = ref) } />;
  38. const verifyResetCode = (<button key="verifyResetCode" onClick={ this.verifyResetCode }>
  39. Verify reset code
  40. </button>);
  41. const newPasswordInput = <CustomInput key="newPassword" type="password" name="newPassword" label="New password" placeholder="New password" onRef={ ref => (this.input.newPassword = ref) } />;
  42. const newPasswordAgainInput = <CustomInput key="newPasswordAgain" type="password" name="newPasswordAgain" label="New password again" placeholder="New password again" onRef={ ref => (this.input.newPasswordAgain = ref) } />;
  43. const changePassword = (<button key="changePassword" onClick={ this.changePassword }>
  44. Change password
  45. </button>);
  46. if (this.state.step === 1) {
  47. return [emailInput, requestResetCodeButton, iAlreadyHaveAResetCodeButton];
  48. } else if (this.state.step === 2) {
  49. return [resetCodeInput, verifyResetCode];
  50. } return [newPasswordInput, newPasswordAgainInput, changePassword];
  51. };
  52. requestResetCode = () => {
  53. if (CustomInput.hasInvalidInput(this.input, ["email"])) {
  54. this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  55. } else {
  56. this.errors.clearErrors();
  57. io.getSocket(socket => {
  58. socket.emit("users.requestPasswordReset", this.input.email.getValue(), res => {
  59. if (res.status === "success") {
  60. alert("Success!");
  61. this.setState({
  62. step: 2,
  63. });
  64. } else {
  65. this.errors.addError(res.message);
  66. }
  67. });
  68. });
  69. }
  70. };
  71. verifyResetCode = () => {
  72. if (CustomInput.hasInvalidInput(this.input, ["resetCode"])) {
  73. this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  74. } else {
  75. this.errors.clearErrors();
  76. io.getSocket(socket => {
  77. socket.emit("users.verifyPasswordResetCode", this.input.resetCode.getValue(), res => {
  78. if (res.status === "success") {
  79. alert("Success!");
  80. this.setState({
  81. step: 3,
  82. resetCode: this.input.resetCode.getValue(),
  83. });
  84. } else {
  85. this.errors.addError(res.message);
  86. }
  87. });
  88. });
  89. }
  90. };
  91. changePassword = () => {
  92. if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) {
  93. this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  94. } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) {
  95. this.errors.clearAddError("New password and new password again need to be the same.");
  96. } else {
  97. this.errors.clearErrors();
  98. io.getSocket(socket => {
  99. socket.emit("users.changePasswordWithResetCode", this.state.resetCode, this.input.newPassword.getValue(), res => {
  100. if (res.status === "success") {
  101. alert("Success!");
  102. location.href = "/login";
  103. } else {
  104. this.errors.addError(res.message);
  105. }
  106. });
  107. });
  108. }
  109. };
  110. skipRequestResetCode = () => {
  111. this.setState({
  112. step: 2,
  113. });
  114. };
  115. render() {
  116. return (
  117. <div>
  118. <h1>Reset password</h1>
  119. <div className="steps">
  120. <span className={ `step-circle-1 ${ this.state.step === 1 ? "step-circle-active" : "" }` }>1</span>
  121. <span className="step-line-1" />
  122. <span className={ `step-circle-2 ${ this.state.step === 2 ? "step-circle-active" : "" }` }>2</span>
  123. <span className="step-line-2" />
  124. <span className={ `step-circle-3 ${ this.state.step === 3 ? "step-circle-active" : "" }` }>3</span>
  125. </div>
  126. <CustomErrors onRef={ ref => (this.errors = ref) } />
  127. { this.getActions() }
  128. </div>
  129. );
  130. }
  131. }