Rename user context as token context and move to a global context.js

This commit is contained in:
Miguel de la Cruz 2021-09-13 11:24:36 +02:00
parent 57dbfc8a87
commit 612f96a9c5
5 changed files with 16 additions and 18 deletions

3
webapp/src/context.js Normal file
View file

@ -0,0 +1,3 @@
import React from 'react'
export const TokenContext = React.createContext()

View file

@ -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>
<Router>
<CssBaseline />
<Switch>
<TokenContext.Provider value={{ token, setToken }}>
<Route path="/login">
<Login />
</Route>
@ -41,9 +41,9 @@ const App = () => {
<Home />
</Secure>
</Route>
</Switch>
</Router>
</UserContext.Provider>
</TokenContext.Provider>
</Switch>
</Router>
)
}

View file

@ -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()

View file

@ -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='/' />

View file

@ -1,5 +0,0 @@
import React from 'react'
const UserContext = React.createContext()
export default UserContext