index.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 CustomMessages from "components/CustomMessages.jsx";
  7. import io from "io";
  8. @connect(state => ({
  9. user: {
  10. userId: state.user.get("userId"),
  11. },
  12. }))
  13. @translate(["setPassword"], { wait: true })
  14. export default class SetPassword extends Component {
  15. static propTypes = {
  16. user: PropTypes.object,
  17. t: PropTypes.func,
  18. };
  19. static defaultProps = {
  20. user: {
  21. userId: "",
  22. },
  23. t: () => {},
  24. };
  25. constructor(props) {
  26. super(props);
  27. CustomInput.initialize(this);
  28. this.state = {
  29. passwordLinked: false,
  30. step: 1,
  31. };
  32. io.getSocket(socket => {
  33. socket.emit("users.findBySession", res => {
  34. if (res.status === "success") {
  35. this.setState({
  36. passwordLinked: res.data.password,
  37. });
  38. } else {
  39. this.messages.addError(this.props.t("general:notLoggedInError"));
  40. }
  41. });
  42. socket.on("event:user.linkPassword", () => {
  43. this.messages.clearAddInfo(this.props.t("setPassword:passwordHasBeenSet"));
  44. location.href = "/settings";
  45. });
  46. });
  47. }
  48. getActions = () => {
  49. const requestCodeButton = (<button key="requestCode" onClick={ this.requestCode }>
  50. { this.props.t("setPassword:requestCode") }
  51. </button>);
  52. const codeInput = <CustomInput key="code" type="uniqueCode" name="code" label={ this.props.t("general:codeInput") } placeholder={ this.props.t("general:codeInput") } onRef={ ref => (this.input.code = ref) } />;
  53. const verifyCodeButton = (<button key="verifyCode" onClick={ this.verifyCode }>
  54. { this.props.t("verifyCode") }
  55. </button>);
  56. 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) } />;
  57. 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) } />;
  58. const setPassword = (<button key="setPassword" onClick={ this.setPassword }>
  59. { this.props.t("setPassword:setPassword") }
  60. </button>);
  61. if (this.state.step === 1) {
  62. return [requestCodeButton];
  63. } if (this.state.step === 2) {
  64. return [codeInput, verifyCodeButton];
  65. } return [newPasswordInput, newPasswordAgainInput, setPassword];
  66. };
  67. setPassword = () => {
  68. this.messages.clearErrorSuccess();
  69. if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) {
  70. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  71. } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) {
  72. this.messages.clearAddError(this.props.t("general:newPasswordNewPasswordAgainSameError"));
  73. } else {
  74. io.getSocket(socket => {
  75. socket.emit("users.changePasswordWithCode", this.state.code, this.input.newPassword.getValue(), res => {
  76. if (res.status === "success") {
  77. this.messages.clearAddSuccess(this.props.t("setPassword:successfullySetPassword"));
  78. location.href = "/settings";
  79. } else {
  80. this.messages.addError(res.message);
  81. }
  82. });
  83. });
  84. }
  85. };
  86. requestCode = () => {
  87. this.messages.clearErrorSuccess();
  88. io.getSocket(socket => {
  89. socket.emit("users.requestPassword", res => {
  90. if (res.status === "success") {
  91. this.messages.clearAddSuccess(this.props.t("setPassword:successfullyRequestedCode"));
  92. this.messages.clearAddInfo(this.props.t("setPassword:weHaveSentAUniqueCode"));
  93. this.setState({
  94. step: 2,
  95. });
  96. } else {
  97. this.messages.addError(res.message);
  98. }
  99. });
  100. });
  101. };
  102. verifyCode = () => {
  103. this.messages.clearErrorSuccess();
  104. if (CustomInput.hasInvalidInput(this.input, ["code"])) {
  105. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  106. } else {
  107. io.getSocket(socket => {
  108. socket.emit("users.verifyPasswordCode", this.input.code.getValue(), res => {
  109. if (res.status === "success") {
  110. this.messages.clearAddSuccess(this.props.t("setPassword:successfullyVerifiedCode"));
  111. this.setState({
  112. step: 3,
  113. code: this.input.code.getValue(),
  114. });
  115. } else {
  116. this.messages.addError(res.message);
  117. }
  118. });
  119. });
  120. }
  121. };
  122. render() {
  123. const { t } = this.props;
  124. return (
  125. <main id="setPassword">
  126. <h1>{ t("setPassword:title") }</h1>
  127. <CustomMessages onRef={ ref => (this.messages = ref) } />
  128. { this.getActions() }
  129. </main>
  130. );
  131. }
  132. }