AuthRoute.jsx 2.9 KB

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