Login.jsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, { Component } from "react";
  2. import CustomInput from "./CustomInput.jsx";
  3. import CustomErrors from "./CustomErrors.jsx";
  4. import io from "../../io";
  5. import config from "../../../../config/default";
  6. export default class Login extends Component {
  7. constructor() {
  8. super();
  9. this.state = {
  10. password: "",
  11. email: "",
  12. errors: [],
  13. inputInvalid: {
  14. email: true,
  15. password: true,
  16. },
  17. };
  18. this.login = this.login.bind(this);
  19. }
  20. updateField(field, event) {
  21. this.setState({
  22. [field]: event.target.value,
  23. });
  24. }
  25. login() {
  26. if (CustomInput.hasInvalidInput(this.state.inputInvalid)) {
  27. alert("Input invalid. Fix before continuing.");
  28. } else {
  29. this.setState({ errors: [] });
  30. io.getSocket(socket => {
  31. socket.emit("users.login", this.state.email, this.state.password, res => {
  32. if (res.status === "success") {
  33. const date = new Date();
  34. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  35. const secure = (config.cookie.secure) ? "secure=true; " : "";
  36. let domain = "";
  37. if (config.cookie.domain !== "localhost") domain = ` domain=${ config.cookie.domain };`;
  38. document.cookie = `SID=${ res.SID }; expires=${ date.toGMTString() }; ${ domain }${ secure }path=/`;
  39. location.reload(); // if we could avoid this, then that would be better
  40. } else {
  41. this.setState({ errors: [res.message] });
  42. }
  43. });
  44. });
  45. }
  46. }
  47. githubRedirect() {
  48. localStorage.setItem("github_redirect", window.location.pathname);
  49. }
  50. validationCallback = CustomInput.validationCallback(this);
  51. render() {
  52. return (
  53. <div>
  54. <CustomErrors errors={ this.state.errors } />
  55. <CustomInput label="Email" placeholder="Email" inputType="email" type="email" name="email" value={ this.state.email } customInputEvents={ { onChange: event => this.updateField("email", event) } } validationCallback={ this.validationCallback } />
  56. <CustomInput label="Password" placeholder="Password" inputType="password" type="password" name="password" value={ this.state.password } customInputEvents={ { onChange: event => this.updateField("password", event) } } validationCallback={ this.validationCallback } />
  57. <p>By logging in/registering you agree to our <a href="/terms">Terms of Service</a> and <a href="/privacy">Privacy Policy</a>.</p>
  58. <button onClick={ this.login }>Login</button>
  59. <a href={ `${ config.serverDomain }/auth/github/authorize` } onClick={ this.githubRedirect }>
  60. <img alt="GitHub Icon" src="/assets/images/social/github.svg" /> &nbsp;&nbsp;Login with GitHub
  61. </a>
  62. <a href="/reset_password">Forgot password?</a>
  63. </div>
  64. );
  65. }
  66. }