mirror of
https://github.com/ditkrg/AuthorizationServerDemos.git
synced 2026-01-23 03:36:52 +00:00
Fixes the Router situation
This commit is contained in:
parent
e452802d13
commit
e0c8e47112
@ -10,25 +10,13 @@ import userManager, { loadUserFromStorage } from "./services/userService";
|
|||||||
import AuthProvider from "./utils/authProvider";
|
import AuthProvider from "./utils/authProvider";
|
||||||
import PrivateRoute from "./utils/protectedRoute";
|
import PrivateRoute from "./utils/protectedRoute";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import { storeUser } from "./actions/authActions";
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Commented out not to check the localStorage for the user's existence ** Temporary **
|
|
||||||
// fetch current user from cookies
|
// fetch current user from cookies
|
||||||
loadUserFromStorage(store);
|
loadUserFromStorage(store);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Should be removed ** Temporary **
|
|
||||||
//const logUserIn = () => {
|
|
||||||
// const user = { profile: { given_name: "John Doe" } };
|
|
||||||
// console.log(`User logged in!`);
|
|
||||||
// store.dispatch(storeUser(user));
|
|
||||||
// };
|
|
||||||
|
|
||||||
// ** Temporary **
|
|
||||||
// logUserIn();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
<AuthProvider userManager={userManager} store={store}>
|
<AuthProvider userManager={userManager} store={store}>
|
||||||
|
|||||||
@ -28,7 +28,7 @@ const VehicleRegisterForm = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card styles={{ padding: "100px 90px 100px 90px" }} classes="xl:w-2/5">
|
<Card styles={{ padding: "50px" }} classes="xl:w-2/5">
|
||||||
<form onSubmit={(e) => e.preventDefault()} className="m-0 flex flex-col">
|
<form onSubmit={(e) => e.preventDefault()} className="m-0 flex flex-col">
|
||||||
<Heading3 styles={{ marginTop: "0" }} classes="font-playfair">
|
<Heading3 styles={{ marginTop: "0" }} classes="font-playfair">
|
||||||
Register Real Estate
|
Register Real Estate
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Switch, Route, BrowserRouter as Router } from "react-router-dom";
|
import { Switch, Route } from "react-router-dom";
|
||||||
import RegisterVehicle from "./RegisterVehicle";
|
import RegisterVehicle from "./RegisterVehicle";
|
||||||
import YourVehicles from "./YourVehicles";
|
import YourVehicles from "./YourVehicles";
|
||||||
|
|
||||||
function Home() {
|
function Home() {
|
||||||
return (
|
return (
|
||||||
<Router>
|
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route path="/register">
|
<Route path="/register">
|
||||||
<RegisterVehicle />
|
<RegisterVehicle />
|
||||||
@ -14,7 +13,6 @@ function Home() {
|
|||||||
<YourVehicles />
|
<YourVehicles />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</Router>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Redirect } from "react-router-dom";
|
import { useLocation, useHistory } from "react-router-dom";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import Navbar from "./components/Navbar";
|
import Navbar from "./components/Navbar";
|
||||||
import Main from "./components/Main";
|
import Main from "./components/Main";
|
||||||
@ -8,13 +8,22 @@ import LoginWindow from "./components/LoginWindow";
|
|||||||
function Login() {
|
function Login() {
|
||||||
const user = useSelector((state) => state.auth.user);
|
const user = useSelector((state) => state.auth.user);
|
||||||
|
|
||||||
return user ? (
|
const location = useLocation();
|
||||||
<Redirect to={"/"} />
|
const history = useHistory();
|
||||||
) : (
|
|
||||||
|
let { from } = location.state || { from: { pathname: "/" } };
|
||||||
|
|
||||||
|
if (user) {
|
||||||
|
history.replace(from);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
!user && (
|
||||||
<div>
|
<div>
|
||||||
<Navbar />
|
<Navbar />
|
||||||
<Main Component={LoginWindow} />
|
<Main Component={LoginWindow} />
|
||||||
</div>
|
</div>
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,27 @@
|
|||||||
import React from 'react'
|
import React from "react";
|
||||||
import { Route, Redirect } from 'react-router-dom'
|
import { Route, Redirect } from "react-router-dom";
|
||||||
import { useSelector } from 'react-redux'
|
import { useSelector } from "react-redux";
|
||||||
|
|
||||||
function ProtectedRoute({ children, component: Component, ...rest }) {
|
function ProtectedRoute({ children, component: Component, ...rest }) {
|
||||||
const user = useSelector(state => state.auth.user)
|
const user = useSelector((state) => state.auth.user);
|
||||||
|
|
||||||
return user
|
return (
|
||||||
? (<Route {...rest} component={Component} />)
|
<Route
|
||||||
: (<Redirect to={'/login'} />)
|
{...rest}
|
||||||
|
render={({ location }) =>
|
||||||
|
user ? (
|
||||||
|
<Component />
|
||||||
|
) : (
|
||||||
|
<Redirect
|
||||||
|
to={{
|
||||||
|
pathname: "/login",
|
||||||
|
state: { from: location },
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ProtectedRoute
|
export default ProtectedRoute;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user