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 { CssBaseline } from '@material-ui/core';
|
||||||
|
|
||||||
import UserContext from './user-context'
|
import { TokenContext } from './context'
|
||||||
import Login from './pages/login'
|
import Login from './pages/login'
|
||||||
import Home from './pages/home'
|
import Home from './pages/home'
|
||||||
|
|
||||||
|
|
||||||
const Secure = ({children}) => {
|
const Secure = ({children}) => {
|
||||||
const { token } = useContext(UserContext)
|
const { token } = useContext(TokenContext)
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return <Redirect to="/login" />
|
return <Redirect to="/login" />
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,10 @@ const App = () => {
|
||||||
const [token, setToken] = useState(localStorage.getItem('token'))
|
const [token, setToken] = useState(localStorage.getItem('token'))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserContext.Provider value={{ token, setToken }}>
|
<Router>
|
||||||
<Router>
|
<CssBaseline />
|
||||||
<CssBaseline />
|
<Switch>
|
||||||
<Switch>
|
<TokenContext.Provider value={{ token, setToken }}>
|
||||||
<Route path="/login">
|
<Route path="/login">
|
||||||
<Login />
|
<Login />
|
||||||
</Route>
|
</Route>
|
||||||
|
@ -41,9 +41,9 @@ const App = () => {
|
||||||
<Home />
|
<Home />
|
||||||
</Secure>
|
</Secure>
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</TokenContext.Provider>
|
||||||
</Router>
|
</Switch>
|
||||||
</UserContext.Provider>
|
</Router>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,11 @@ import {
|
||||||
Button
|
Button
|
||||||
} from '@material-ui/core';
|
} from '@material-ui/core';
|
||||||
|
|
||||||
import UserContext from '../user-context'
|
import { TokenContext } from '../context'
|
||||||
|
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
const { token, setToken } = useContext(UserContext)
|
const { token, setToken } = useContext(TokenContext)
|
||||||
|
|
||||||
const handleLogout = (e) => {
|
const handleLogout = (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
|
@ -7,10 +7,10 @@ import {
|
||||||
} from '@material-ui/core';
|
} from '@material-ui/core';
|
||||||
|
|
||||||
import client from '../client'
|
import client from '../client'
|
||||||
import UserContext from '../user-context'
|
import { TokenContext } from '../context'
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const { token, setToken } = useContext(UserContext)
|
const { token, setToken } = useContext(TokenContext)
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
return <Redirect to='/' />
|
return <Redirect to='/' />
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
import React from 'react'
|
|
||||||
|
|
||||||
const UserContext = React.createContext()
|
|
||||||
|
|
||||||
export default UserContext
|
|
Loading…
Reference in a new issue