SetPassword.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. passwordLinked: false,
  26. step: 1,
  27. };
  28. io.getSocket(socket => {
  29. socket.emit("users.findBySession", res => {
  30. if (res.status === "success") {
  31. this.setState({
  32. passwordLinked: res.data.password,
  33. });
  34. } else {
  35. this.errors.addError("You are currently not logged in.");
  36. }
  37. });
  38. socket.on("event:user.linkPassword", () => {
  39. alert("A password for your account has been set. We will now redirect you to the settings page.");
  40. location.href = "/settings";
  41. });
  42. });
  43. }
  44. getActions = () => {
  45. const requestCodeButton = (<button key="requestCode" onClick={ this.requestCode }>
  46. Request code
  47. </button>);
  48. const codeInput = <CustomInput key="code" type="uniqueCode" name="code" label="Code" placeholder="Code" onRef={ ref => (this.input.code = ref) } />;
  49. const verifyCodeButton = (<button key="verifyCode" onClick={ this.verifyCode }>
  50. Verify code
  51. </button>);
  52. const newPasswordInput = <CustomInput key="newPassword" type="password" name="newPassword" label="New password" placeholder="New password" onRef={ ref => (this.input.newPassword = ref) } />;
  53. const newPasswordAgainInput = <CustomInput key="newPasswordAgain" type="password" name="newPasswordAgain" label="New password again" placeholder="New password again" onRef={ ref => (this.input.newPasswordAgain = ref) } />;
  54. const setPassword = (<button key="setPassword" onClick={ this.setPassword }>
  55. Change password
  56. </button>);
  57. if (this.state.step === 1) {
  58. return [requestCodeButton];
  59. } if (this.state.step === 2) {
  60. return [codeInput, verifyCodeButton];
  61. } return [newPasswordInput, newPasswordAgainInput, setPassword];
  62. };
  63. setPassword = () => {
  64. if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) {
  65. this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  66. } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) {
  67. this.errors.clearAddError("New password and new password again need to be the same.");
  68. } else {
  69. this.errors.clearErrors();
  70. io.getSocket(socket => {
  71. socket.emit("users.changePasswordWithCode", this.state.code, this.input.newPassword.getValue(), res => {
  72. if (res.status === "success") {
  73. alert("Success!");
  74. location.href = "/settings";
  75. } else {
  76. this.errors.addError(res.message);
  77. }
  78. });
  79. });
  80. }
  81. };
  82. requestCode = () => {
  83. this.errors.clearErrors();
  84. io.getSocket(socket => {
  85. socket.emit("users.requestPassword", res => {
  86. if (res.status === "success") {
  87. alert("Success!");
  88. this.setState({
  89. step: 2,
  90. });
  91. } else {
  92. this.errors.addError(res.message);
  93. }
  94. });
  95. });
  96. };
  97. verifyCode = () => {
  98. if (CustomInput.hasInvalidInput(this.input, ["code"])) {
  99. this.errors.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  100. } else {
  101. this.errors.clearErrors();
  102. io.getSocket(socket => {
  103. socket.emit("users.verifyPasswordCode", this.input.code.getValue(), res => {
  104. if (res.status === "success") {
  105. alert("Success!");
  106. this.setState({
  107. step: 3,
  108. code: this.input.code.getValue(),
  109. });
  110. } else {
  111. this.errors.addError(res.message);
  112. }
  113. });
  114. });
  115. }
  116. };
  117. render() {
  118. return (
  119. <div>
  120. <h1>Set Password</h1>
  121. <CustomErrors onRef={ ref => (this.errors = ref) } />
  122. { this.getActions() }
  123. </div>
  124. );
  125. }
  126. }