-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrivateRoute.js
38 lines (32 loc) · 1.21 KB
/
PrivateRoute.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React from 'react'
import { Route, Redirect } from 'react-router-dom'
//* Trivial Method
//* On successful login - retrieve JWT token
//* Store JWT token in httpOnly cookie or localStorage
//* Setup a `request interceptor` using axios
//* Using the `request interceptor` add JWT token in the header of every API call
//* If JWT token is valid, you either get results/errors
//* Define a error code for invalid JWT , say 402, use only for invalid JWT
//* Setup `response interceptor` using axios
//* Using `response interceptor` redirect to login page for 402
//* performValidationHere
//* Have a context value - boolean
//* Use the context boolean for validation
const PrivateRoute = ({component: Component, ...rest}) => {
const condition = performValidationHere()
return (
<Route
{...rest}
render = {
(routeProps) => (
condition
?
<Component {...routeProps} {...rest} />
:
<Redirect to = '/login' />
)
}
/>
)
}
export default PrivateRoute