index.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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(["login"], { wait: true })
  9. export default class Login 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. }
  20. login = () => {
  21. this.messages.clearErrorSuccess();
  22. if (CustomInput.hasInvalidInput(this.input)) {
  23. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  24. } else {
  25. io.getSocket(socket => {
  26. socket.emit("users.login", this.input.email.getValue(), this.input.password.getValue(), res => {
  27. if (res.status === "success") {
  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. let domain = "";
  32. if (config.cookie.domain !== "localhost") domain = ` domain=${ config.cookie.domain };`;
  33. document.cookie = `${ config.cookie.sidName }=${ res.SID }; expires=${ date.toGMTString() }; ${ domain }${ secure }path=/`;
  34. location.reload(); // if we could avoid this, then that would be better
  35. } else {
  36. this.messages.addError(res.message);
  37. }
  38. });
  39. });
  40. }
  41. }
  42. githubRedirect() {
  43. localStorage.setItem("github_redirect", window.location.pathname);
  44. }
  45. render() {
  46. const { t } = this.props;
  47. return (
  48. <div>
  49. <h1>{ t("login:title") }</h1>
  50. <CustomMessages onRef={ ref => (this.messages = ref) } />
  51. <CustomInput type="email" name="email" label={ t("general:emailInput") } placeholder={ t("general:emailInput") } onRef={ ref => (this.input.email = ref) } />
  52. <CustomInput type="password" name="password" label={ t("general:passwordInput") } placeholder={ t("general:passwordInput") } onRef={ ref => (this.input.password = ref) } />
  53. <p>{ t("login:byLoggingIn", { termsOfService: <a href="/terms">{ t("general:termsOfService") }</a>, privacyPolicy: <a href="/privacy">{ t("general:privacyPolicy") }</a> }) }</p>
  54. <button onClick={ this.login }>{ t("login:login") }</button>
  55. <a href={ `${ config.serverDomain }/auth/github/authorize` } onClick={ this.githubRedirect }>
  56. { t("login:loginWithGitHub") }
  57. </a>
  58. <a href="/reset_password">{ t("login:forgotPassword") }</a>
  59. </div>
  60. );
  61. }
  62. }