index.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { Component } from "react";
  2. import CustomInput from "components/CustomInput.jsx";
  3. import CustomMessages from "components/CustomMessages.jsx";
  4. import io from "io";
  5. import config from "config";
  6. export default class Register extends Component {
  7. constructor() {
  8. super();
  9. CustomInput.initialize(this);
  10. this.state = {
  11. recaptcha: "",
  12. };
  13. }
  14. componentDidMount() {
  15. this.state.recaptcha = grecaptcha.render("recaptcha", {
  16. "sitekey": config.recaptcha.key,
  17. });
  18. }
  19. register = () => {
  20. this.messages.clearErrorSuccess();
  21. if (CustomInput.hasInvalidInput(this.input)) {
  22. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  23. } else {
  24. io.getSocket(socket => {
  25. socket.emit("users.register", this.input.username.getValue(), this.input.email.getValue(), this.input.password.getValue(), grecaptcha.getResponse(this.state.recaptcha), res => {
  26. if (res.status === "success") {
  27. if (res.SID) {
  28. const date = new Date();
  29. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  30. const secure = (config.cookie.secure) ? "secure=true; " : "";
  31. document.cookie = `${ config.cookie.sidName }=${ res.SID }; expires=${ date.toGMTString() }; domain=${ config.cookie.domain }; ${ secure }path=/`;
  32. location.reload(); // if we could avoid this, then that would be better
  33. } else {
  34. // redirect to login
  35. }
  36. } else {
  37. this.messages.addError(res.message);
  38. grecaptcha.reset();
  39. }
  40. });
  41. });
  42. }
  43. };
  44. githubRedirect() {
  45. localStorage.setItem("github_redirect", window.location.pathname);
  46. }
  47. render() {
  48. return (
  49. <div>
  50. <CustomMessages onRef={ ref => (this.messages = ref) } />
  51. <CustomInput type="email" name="email" label="Email" placeholder="Email" onRef={ ref => (this.input.email = ref) } />
  52. <CustomInput type="username" name="username" label="Username" placeholder="Username" onRef={ ref => (this.input.username = ref) } />
  53. <CustomInput type="password" name="password" label="Password" placeholder="Password" onRef={ ref => (this.input.password = ref) } />
  54. <div id="recaptcha" />
  55. <p>By logging in/registering you agree to our <a href="/terms">Terms of Service</a> and <a href="/privacy">Privacy Policy</a>.</p>
  56. <button onClick={ this.register }>Register</button>
  57. <a href={ `${ config.serverDomain }/auth/github/authorize` } onClick={ this.githubRedirect }>
  58. <div className="icon">
  59. <img alt="GitHub Icon" src="/assets/images/social/github.svg" />
  60. </div>
  61. &nbsp;&nbsp;Login with GitHub
  62. </a>
  63. </div>
  64. );
  65. }
  66. }