Skip to content

Commit

Permalink
finished protected route demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Orang-utan committed Dec 26, 2020
1 parent 6292d4e commit e7393be
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import auth from './api/auth';
import Navbar from './components/Navbar';
import PublicRoute from './components/routing/PublicRoute';
import PrivateRoute from './components/routing/PrivateRoute';
import { AuthContext } from './context';

// import pages
import LoginPage from './pages/authflow/LoginPage';
import RegisterPage from './pages/authflow/RegisterPage';
import IndexPage from './pages/IndexPage';
import NotFoundPage from './pages/NotFoundPage';
import DashboardPage from './pages/DashboardPage';

const queryCache = new QueryCache();

Expand Down Expand Up @@ -47,6 +51,7 @@ function App() {
<PublicRoute exact path="/" component={IndexPage} />
<PublicRoute exact path="/login" component={LoginPage} />
<PublicRoute exact path="/register" component={RegisterPage} />
<PrivateRoute exact path="/dashboard" component={DashboardPage} />
<Route exact={false} component={NotFoundPage} />
</Switch>
</main>
Expand Down
9 changes: 2 additions & 7 deletions src/client/src/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@ class Auth {

/** Logout from account. */
async logout() {
try {
await api.post('/api/users/logout');
this.accessToken = null;
localStorage.removeItem('authRefreshToken');
} catch (err) {
console.log(err);
}
this.accessToken = null;
localStorage.removeItem('authRefreshToken');
}

/** Refresh access token using saved refresh token. */
Expand Down
2 changes: 1 addition & 1 deletion src/client/src/components/routing/PublicRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const PublicRoute = (props) => {
const auth = useContext(AuthContext);

return auth.isAuthenticated ? (
<Redirect to="/" />
<Redirect to="/dashboard" />
) : (
<Route path={props.path} exact={props.exact} component={props.component} />
);
Expand Down
7 changes: 7 additions & 0 deletions src/client/src/pages/DashboardPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

const DashboardPage = () => {
return <div>This is Dashboard</div>;
};

export default DashboardPage;

0 comments on commit e7393be

Please sign in to comment.