Skip to content

Commit

Permalink
feature(nav): Route Guarding #6
Browse files Browse the repository at this point in the history
  • Loading branch information
bip12 committed Jan 11, 2022
1 parent 1018bf5 commit 5b71ce3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions application/src/redux/actions/authActions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { LOGIN, LOGOUT } from './types';
import { SERVER_IP } from '../../private'
import { fakeAuth } from '../../router/appRouter';

const finishLogin = (email, token) => {
if(token==='12345luggage') // fake
{
fakeAuth.signedIn = true;
}
return {
type: LOGIN,
payload: {
Expand Down Expand Up @@ -32,6 +37,7 @@ export const loginUser = (email, password) => {
}

export const logoutUser = () => {
fakeAuth.signedIn = false;
return {
type: LOGOUT,
payload: null,
Expand Down
27 changes: 22 additions & 5 deletions application/src/router/appRouter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import { Main, Login, OrderForm, ViewOrders} from '../components';
import RegisterForm from '../components/register/registerForm';

// fake authentication
export let fakeAuth = {
signedIn: false
};

const RequireAuth = ({ children }) => {
if (!fakeAuth.signedIn) {
return <Redirect to="/login" />;
}
return children;
};

const AppRouter = (props) => {
return (
<Router>
<Route path="/" exact component={Main} />
<Route path="/login" exact component={Login} />
<Route path="/order" exact component={OrderForm} />
<Route path="/view-orders" exact component={ViewOrders} />
<Switch>
<Route path="/" exact component={Main} />
<Route path="/login" exact component={Login} />
<RequireAuth>
<Route path="/order" exact component={OrderForm} />
<Route path="/view-orders" exact component={ViewOrders} />
</RequireAuth>
</Switch>
</Router>
);
}
Expand Down

0 comments on commit 5b71ce3

Please sign in to comment.