Rename user context as token context and move to a global context.js
This commit is contained in:
parent
57dbfc8a87
commit
612f96a9c5
5 changed files with 16 additions and 18 deletions
3
webapp/src/context.js
Normal file
3
webapp/src/context.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
import React from 'react'
|
||||
|
||||
export const TokenContext = React.createContext()
|
|
@ -10,13 +10,13 @@ import {
|
|||
|
||||
import { CssBaseline } from '@material-ui/core';
|
||||
|
||||
import UserContext from './user-context'
|
||||
import { TokenContext } from './context'
|
||||
import Login from './pages/login'
|
||||
import Home from './pages/home'
|
||||
|
||||
|
||||
const Secure = ({children}) => {
|
||||
const { token } = useContext(UserContext)
|
||||
const { token } = useContext(TokenContext)
|
||||
if (!token) {
|
||||
return <Redirect to="/login" />
|
||||
}
|
||||
|
@ -28,10 +28,10 @@ const App = () => {
|
|||
const [token, setToken] = useState(localStorage.getItem('token'))
|
||||
|
||||
return (
|
||||
<UserContext.Provider value={{ token, setToken }}>
|
||||
<Router>
|
||||
<CssBaseline />
|
||||
<Switch>
|
||||
<TokenContext.Provider value={{ token, setToken }}>
|
||||
<Route path="/login">
|
||||
<Login />
|
||||
</Route>
|
||||
|
@ -41,9 +41,9 @@ const App = () => {
|
|||
<Home />
|
||||
</Secure>
|
||||
</Route>
|
||||
</TokenContext.Provider>
|
||||
</Switch>
|
||||
</Router>
|
||||
</UserContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -4,11 +4,11 @@ import {
|
|||
Button
|
||||
} from '@material-ui/core';
|
||||
|
||||
import UserContext from '../user-context'
|
||||
import { TokenContext } from '../context'
|
||||
|
||||
|
||||
const Home = () => {
|
||||
const { token, setToken } = useContext(UserContext)
|
||||
const { token, setToken } = useContext(TokenContext)
|
||||
|
||||
const handleLogout = (e) => {
|
||||
e.preventDefault()
|
||||
|
|
|
@ -7,10 +7,10 @@ import {
|
|||
} from '@material-ui/core';
|
||||
|
||||
import client from '../client'
|
||||
import UserContext from '../user-context'
|
||||
import { TokenContext } from '../context'
|
||||
|
||||
const Login = () => {
|
||||
const { token, setToken } = useContext(UserContext)
|
||||
const { token, setToken } = useContext(TokenContext)
|
||||
|
||||
if (token) {
|
||||
return <Redirect to='/' />
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
import React from 'react'
|
||||
|
||||
const UserContext = React.createContext()
|
||||
|
||||
export default UserContext
|
Loading…
Reference in a new issue