index.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { connect } from "react-redux";
  4. import CustomInput from "components/CustomInput.jsx";
  5. import CustomMessages from "components/CustomMessages.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.messages.addError("You are currently not logged in.");
  36. }
  37. });
  38. socket.on("event:user.linkPassword", () => {
  39. this.messages.clearAddInfo("A password for your account has been set. Redirecting 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. this.messages.clearErrorSuccess();
  65. if (CustomInput.hasInvalidInput(this.input, ["newPassword", "newPasswordAgain"])) {
  66. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  67. } else if (CustomInput.isTheSame(this.input, ["newPassword", "newPasswordAgain"])) {
  68. this.messages.clearAddError("New password and new password again need to be the same.");
  69. } else {
  70. io.getSocket(socket => {
  71. socket.emit("users.changePasswordWithCode", this.state.code, this.input.newPassword.getValue(), res => {
  72. if (res.status === "success") {
  73. this.messages.clearAddSuccess("Successfully set password. Redirecting you to the settings page.");
  74. location.href = "/settings";
  75. } else {
  76. this.messages.addError(res.message);
  77. }
  78. });
  79. });
  80. }
  81. };
  82. requestCode = () => {
  83. this.messages.clearErrorSuccess();
  84. io.getSocket(socket => {
  85. socket.emit("users.requestPassword", res => {
  86. if (res.status === "success") {
  87. this.messages.clearAddSuccess("Successfully requested code.");
  88. this.messages.clearAddInfo("We have sent a unique code to your email address.");
  89. this.setState({
  90. step: 2,
  91. });
  92. } else {
  93. this.messages.addError(res.message);
  94. }
  95. });
  96. });
  97. };
  98. verifyCode = () => {
  99. this.messages.clearErrorSuccess();
  100. if (CustomInput.hasInvalidInput(this.input, ["code"])) {
  101. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  102. } else {
  103. io.getSocket(socket => {
  104. socket.emit("users.verifyPasswordCode", this.input.code.getValue(), res => {
  105. if (res.status === "success") {
  106. this.messages.clearAddSuccess("Successfully verified code.");
  107. this.setState({
  108. step: 3,
  109. code: this.input.code.getValue(),
  110. });
  111. } else {
  112. this.messages.addError(res.message);
  113. }
  114. });
  115. });
  116. }
  117. };
  118. render() {
  119. return (
  120. <div>
  121. <h1>Set Password</h1>
  122. <CustomMessages onRef={ ref => (this.messages = ref) } />
  123. { this.getActions() }
  124. </div>
  125. );
  126. }
  127. }