index.jsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 Login extends Component {
  7. constructor() {
  8. super();
  9. CustomInput.initialize(this);
  10. }
  11. login = () => {
  12. this.messages.clearErrorSuccess();
  13. if (CustomInput.hasInvalidInput(this.input)) {
  14. this.messages.clearAddError("Some fields are incorrect. Please fix them before continuing.");
  15. } else {
  16. io.getSocket(socket => {
  17. socket.emit("users.login", this.input.email.getValue(), this.input.password.getValue(), res => {
  18. if (res.status === "success") {
  19. const date = new Date();
  20. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  21. const secure = (config.cookie.secure) ? "secure=true; " : "";
  22. let domain = "";
  23. if (config.cookie.domain !== "localhost") domain = ` domain=${ config.cookie.domain };`;
  24. document.cookie = `${ config.cookie.sidName }=${ res.SID }; expires=${ date.toGMTString() }; ${ domain }${ secure }path=/`;
  25. location.reload(); // if we could avoid this, then that would be better
  26. } else {
  27. this.messages.addError(res.message);
  28. }
  29. });
  30. });
  31. }
  32. }
  33. githubRedirect() {
  34. localStorage.setItem("github_redirect", window.location.pathname);
  35. }
  36. render() {
  37. return (
  38. <div>
  39. <CustomMessages onRef={ ref => (this.messages = ref) } />
  40. <CustomInput type="email" name="email" label="Email" placeholder="Email" onRef={ ref => (this.input.email = ref) } />
  41. <CustomInput type="password" name="password" label="Password" placeholder="Password" onRef={ ref => (this.input.password = ref) } />
  42. <p>By logging in/registering you agree to our <a href="/terms">Terms of Service</a> and <a href="/privacy">Privacy Policy</a>.</p>
  43. <button onClick={ this.login }>Login</button>
  44. <a href={ `${ config.serverDomain }/auth/github/authorize` } onClick={ this.githubRedirect }>
  45. <img alt="GitHub Icon" src="/assets/images/social/github.svg" /> &nbsp;&nbsp;Login with GitHub
  46. </a>
  47. <a href="/reset_password">Forgot password?</a>
  48. </div>
  49. );
  50. }
  51. }