ForgotPassword.jsx 4.6 KB

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