Login.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import io from "../../io";
  4. import config from "../../../../config/default";
  5. export default class Login extends Component {
  6. static propTypes = {
  7. password: PropTypes.string,
  8. email: PropTypes.string,
  9. }
  10. constructor() {
  11. super();
  12. this.state = {
  13. password: "",
  14. email: "",
  15. };
  16. this.login = this.login.bind(this);
  17. }
  18. updateField(field, event) {
  19. this.setState({
  20. [field]: event.target.value,
  21. });
  22. }
  23. login() {
  24. io.getSocket(socket => {
  25. socket.emit("users.login", this.state.email, this.state.password, res => {
  26. if (res.status === "success") {
  27. const date = new Date();
  28. date.setTime(new Date().getTime() + (2 * 365 * 24 * 60 * 60 * 1000));
  29. const secure = (config.cookie.secure) ? "secure=true; " : "";
  30. let domain = "";
  31. if (config.cookie.domain !== "localhost") domain = ` domain=${ config.cookie.domain };`;
  32. document.cookie = `SID=${ res.SID }; expires=${ date.toGMTString() }; ${ domain }${ secure }path=/`;
  33. location.reload(); // if we could avoid this, then that would be better
  34. } else {
  35. // return res.message, temporarily:
  36. alert(res.message); // eslint-disable-line no-alert
  37. }
  38. });
  39. });
  40. }
  41. render() {
  42. return (
  43. <div>
  44. <label htmlFor="email">Email</label>
  45. <input type="text" id="email" value={ this.state.email } onChange={ event => this.updateField("email", event) } />
  46. <label htmlFor="password">Password</label>
  47. <input type="password" id="password" value={ this.state.password } onChange={ event => this.updateField("password", event) } />
  48. <button onClick={ this.login }>Login</button>
  49. </div>
  50. );
  51. }
  52. }