This tutorial follows Tutorial 1
Provided you are using react-router-dom the code should still work for you. I will be linking the github repo ASAP
Step1
Setup files needed for tutorial.
- Open your Project in VsCode
a.
cd projects/yourprojectnamehere
b.code .
c. or Just use the file menu - Open the VS Code Terminal and Get the status of git.
a.
git status
1. You should be on the master branch and your working tree should be clean - Checkout to a new branch and set the upstream
a.
git checkout -b <Tutorial-2-PrivateRoute>
b.git push -u origin <Tutorial-2-PrivateRoute>
- Create a PrivateRoute file
a.
touch src/cofigs/PrivateRoute.jsx
- Create a private file
a.
touch pages/admin/AdminHome.jsx
- Add some content to the AdminHome file
import React from 'react'
const AdminHome = () => {
return (
<div>
<h1>Welcome home Admin</h1>
</div>
)
}
export default AdminHome
Step 2
Creating the Private Route File.
- Import React, Route and Redirect
a.
imr
b.imd
c.import { Route as R, Redirect as RD } from 'react-router-dom';
- Setup the arrow fucntion
Note: We are not using |rafce| here because we are bringing in props. (More to be explained later)
a.
const PrivateRoute = ({ component: Component, ...rest }) = {
<R
{...rest}
render={props =>
localStorage.getItem('token') ?
(
<Component {...props} />
):
(
<RD to='/' />
)
}
[/](file:///)>
);
export default PrivateRoute;
Lets Break this down:
Components - let you split and isolate the UI into independent, reuseable pieces they are like JS functions
Props - the inputs that components accept
Render - Display
Ternary - If Else Statement
localStorage - a place where you can store data on a users machine to be used by the site
... - Spread operator - expands a string, object, or array into it's elements
What are we doing?
- We are bringing the component property defined on props from our AppRouter file (we will hook this up in a minute) and assinging it to a new location in state called Component.
- We are then taking any remaining objects defined on props and collecting them in an argument called ...rest
- After this we are setting up a route to render our props to our private componenet IF there is a token in localStorage otherwise, we are redirecting to home.
Step 3
Setting up the AppRouter file to render the PrivateRoute
- Open the AppRouter.jsx file and import PrivateRoute
a.
import PrivateRoute from './PrivateRoute.jsx';
- Import the AdminHome Page
a.
import AdminHome from '../pages/admin/AdminHome.jsx';
- Add a Private Route to the AdminHome Page
a.
<PrivateRoute path='/admin/AdminHome' componenet={AdminHome} />
- Add a NavLink to the AdminHome page
a. open the NavBar.jsx file
b.
<NL to='/admin/adminhome/'>Admin Home<NL/>
Step 4
Tying it all together
- Run your app
a.
npm start
- Open your developer tools (Chrome) Firefox works too a. right click on the page b. click inspect
- Clear Tokens from localStorage if any a. In the top of the developer tool click Application b. To the right click Local Storage c. Open the local storage for the page you are on d. if there is a token delete it (click and press the x)
- Try to navigate to AdminHome a. It should not work and redirect you to Home
- Add a token a. In the place where we checked for tokens double click the key field and type token (be sure this matches what you are calling from local storage) b. Give your token any value
- Navigate to your Admin Home
- Add, Commit, Push, and Pull Request your new private route
Top comments (0)