index.jsx 3.0 KB

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