React Router fixes for Console (#336)

- Adding protectedRoute component
- Removed unnecessary redirect login
This commit is contained in:
Lenin Alevski
2020-10-21 13:13:40 -07:00
committed by GitHub
parent 7e9d581277
commit 0c43e5c3f4
4 changed files with 128 additions and 133 deletions

File diff suppressed because one or more lines are too long

View File

@@ -15,7 +15,13 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import React from "react";
import { Redirect, Route, Router, Switch } from "react-router-dom";
import {
Redirect,
Route,
Router,
Switch,
BrowserRouter,
} from "react-router-dom";
import history from "./history";
import Login from "./screens/LoginPage/LoginPage";
import Console from "./screens/Console/Console";
@@ -27,6 +33,22 @@ import { userLoggedIn } from "./actions";
import LoginCallback from "./screens/LoginPage/LoginCallback";
import { hot } from "react-hot-loader/root";
interface ProtectedRouteProps {
loggedIn: boolean;
component: any;
}
export class ProtectedRoute extends React.Component<ProtectedRouteProps> {
render() {
const Component = this.props.component;
return this.props.loggedIn ? (
<Component />
) : (
<Redirect to={{ pathname: "/login" }} />
);
}
}
const isLoggedIn = () => {
return (
storage.getItem("token") !== undefined &&
@@ -47,29 +69,14 @@ interface RoutesProps {
}
class Routes extends React.Component<RoutesProps> {
componentDidMount(): void {
if (isLoggedIn()) {
this.props.userLoggedIn(true);
}
}
render() {
const loggedIn = isLoggedIn();
return (
<Router history={history}>
<Switch>
<Route exact path="/oauth_callback" component={LoginCallback} />
<Route exact path="/login" component={Login} />
{this.props.loggedIn ? (
<Switch>
<Route path="/*" component={Console} />
<Route component={NotFoundPage} />
</Switch>
) : (
<Switch>
<Route exact path="/" component={Login} />
<Redirect to="/" />
</Switch>
)}
<ProtectedRoute component={Console} loggedIn={loggedIn} />
</Switch>
</Router>
);

View File

@@ -71,19 +71,6 @@ import ObjectBrowser from "./ObjectBrowser/ObjectBrowser";
import ListObjects from "./Buckets/ListBuckets/Objects/ListObjects/ListObjects";
import License from "./License/License";
function Copyright() {
return (
<Typography variant="body2" color="textSecondary" align="center">
{"Copyright © "}
<Link color="inherit" href="https://material-ui.com/">
MinIO
</Link>{" "}
{new Date().getFullYear()}
{"."}
</Typography>
);
}
const drawerWidth = 245;
const styles = (theme: Theme) =>
@@ -385,9 +372,7 @@ const Console = ({
/>
))}
{allowedRoutes.length > 0 ? (
<Route exact path="/">
<Redirect to={allowedRoutes[0].path} />
</Route>
<Redirect to={allowedRoutes[0].path} />
) : null}
</Switch>
</Router>

View File

@@ -19,10 +19,12 @@
package restapi
import (
"bytes"
"crypto/tls"
"log"
"net/http"
"strings"
"time"
"github.com/minio/console/pkg/auth"
@@ -229,8 +231,9 @@ func wrapHandlerSinglePageApplication(h http.Handler) http.HandlerFunc {
nfrw := &notFoundRedirectRespWr{ResponseWriter: w}
h.ServeHTTP(nfrw, r)
if nfrw.status == 404 {
log.Printf("Redirecting %s to index.html.", r.RequestURI)
http.Redirect(w, r, "/index.html", http.StatusFound)
indexPage, _ := portalUI.Asset("build/index.html")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(indexPage))
}
}
}