index.jsx 4.6 KB

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