AuthRoute.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { Component } from "react";
  2. import { connect } from "react-redux";
  3. import PropTypes from "prop-types";
  4. import { Redirect, Route } from "react-router-dom";
  5. import io from "io";
  6. @connect(state => ({
  7. loggedIn: state.user.get("loggedIn"),
  8. role: state.user.get("role"),
  9. authProcessed: state.user.get("authProcessed"),
  10. }))
  11. export default class AuthRoute extends Component {
  12. static propTypes = {
  13. loggedIn: PropTypes.bool,
  14. role: PropTypes.string,
  15. auth: PropTypes.string,
  16. authProcessed: PropTypes.bool,
  17. component: PropTypes.oneOfType([
  18. PropTypes.element,
  19. PropTypes.func,
  20. ]),
  21. computedMatch: PropTypes.object,
  22. };
  23. static defaultProps = {
  24. loggedIn: false,
  25. role: "default",
  26. auth: "ignored",
  27. authProcessed: false,
  28. component: () => {},
  29. computedMatch: {},
  30. };
  31. constructor(props) {
  32. super(props);
  33. const state = {
  34. stationName: props.computedMatch.params.name,
  35. waitingFor: "",
  36. continue: false,
  37. stationData: null,
  38. receivedStationData: false,
  39. };
  40. const { auth } = props;
  41. if (auth === "ignored") state.continue = true;
  42. else if (auth === "station") {
  43. state.waitingFor = "station";
  44. this.getStationData();
  45. } else state.waitingFor = "auth";
  46. this.state = state;
  47. }
  48. getStationData = () => {
  49. io.getSocket(socket => {
  50. socket.emit("stations.findByName", this.state.stationName, res => {
  51. this.setState({
  52. receivedStationData: true,
  53. stationData: res.data,
  54. });
  55. });
  56. });
  57. };
  58. render() {
  59. const { auth, role, loggedIn, authProcessed } = this.props;
  60. const { stationName, waitingFor, receivedStationData, stationData } = this.state;
  61. if (this.state.continue) {
  62. return <Route props={ this.props } component={ this.props.component } />;
  63. } else if (waitingFor === "station" && receivedStationData) {
  64. if (stationData) {
  65. const props = JSON.parse(JSON.stringify(this.props));
  66. // TODO Replace the above hack with a proper Object.clone
  67. props.stationName = stationName;
  68. props.stationData = stationData;
  69. return <Route props={ props } component={ this.props.component } />;
  70. }
  71. return <Redirect to={ "/" } />;
  72. } else if (waitingFor === "auth" && authProcessed) {
  73. if (auth === "required") {
  74. if (loggedIn) return <Route props={ this.props } component={ this.props.component } />;
  75. return <Redirect to={ "/" } />;
  76. } else if (auth === "disallowed") {
  77. if (!loggedIn) return <Route props={ this.props } component={ this.props.component } />;
  78. return <Redirect to={ "/login" } />;
  79. } else if (auth === "admin") {
  80. if (role === "admin") return <Route props={ this.props } component={ this.props.component } />;
  81. return <Redirect to={ "/" } />;
  82. }
  83. }
  84. return <h1>Loading...</h1>;
  85. }
  86. }