index.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { Component } from "react";
  2. import CustomInput from "components/CustomInput.jsx";
  3. import CustomMessages from "components/CustomMessages.jsx";
  4. import PropTypes from "prop-types";
  5. import { translate } from "react-i18next";
  6. import io from "io";
  7. import config from "config";
  8. @translate(["register"], { wait: true })
  9. export default class Register extends Component {
  10. static propTypes = {
  11. t: PropTypes.func,
  12. };
  13. static defaultProps = {
  14. t: () => {},
  15. };
  16. constructor() {
  17. super();
  18. CustomInput.initialize(this);
  19. this.state = {
  20. recaptcha: "",
  21. };
  22. }
  23. componentDidMount() {
  24. this.state.recaptcha = grecaptcha.render("recaptcha", {
  25. "sitekey": config.recaptcha.key,
  26. });
  27. }
  28. register = () => {
  29. this.messages.clearErrorSuccess();
  30. if (CustomInput.hasInvalidInput(this.input)) {
  31. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  32. } else {
  33. io.getSocket(socket => {
  34. socket.emit("users.register", this.input.username.getValue(), this.input.email.getValue(), this.input.password.getValue(), grecaptcha.getResponse(this.state.recaptcha), res => {
  35. if (res.status === "success") {
  36. if (res.SID) {
  37. const date = new Date();
  38. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  39. const secure = (config.cookie.secure) ? "secure=true; " : "";
  40. document.cookie = `${ config.cookie.sidName }=${ res.SID }; expires=${ date.toGMTString() }; domain=${ config.cookie.domain }; ${ secure }path=/`;
  41. location.reload(); // if we could avoid this, then that would be better
  42. } else {
  43. // redirect to login
  44. }
  45. } else {
  46. this.messages.addError(res.message);
  47. grecaptcha.reset();
  48. }
  49. });
  50. });
  51. }
  52. };
  53. githubRedirect() {
  54. localStorage.setItem("github_redirect", window.location.pathname);
  55. }
  56. render() {
  57. const { t } = this.props;
  58. return (
  59. <div>
  60. <h1>{ t("register:title") }</h1>
  61. <CustomMessages onRef={ ref => (this.messages = ref) } />
  62. <CustomInput type="email" name="email" label={ t("general:emailInput") } placeholder={ t("general:emailInput") } onRef={ ref => (this.input.email = ref) } />
  63. <CustomInput type="username" name="username" label={ t("general:usernameInput") } placeholder={ t("general:usernameInput") } onRef={ ref => (this.input.username = ref) } />
  64. <CustomInput type="password" name="password" label={ t("general:passwordInput") } placeholder={ t("general:passwordInput") } onRef={ ref => (this.input.password = ref) } />
  65. <div id="recaptcha" />
  66. <p>{ t("register:byLoggingIn", { termsOfService: <a href="/terms">{ t("general:termsOfService") }</a>, privacyPolicy: <a href="/privacy">{ t("general:privacyPolicy") }</a> }) }</p>
  67. <button onClick={ this.register }>{ t("register:register") }</button>
  68. <a href={ `${ config.serverDomain }/auth/github/authorize` } onClick={ this.githubRedirect }>{ t("register:registerWithGitHub") }</a>
  69. </div>
  70. );
  71. }
  72. }