2
0

index.jsx 2.6 KB

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