diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/PrivateRoute.jsx | 9 | ||||
-rw-r--r-- | src/utils/api.js | 21 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/utils/PrivateRoute.jsx b/src/utils/PrivateRoute.jsx new file mode 100644 index 0000000..b57bcf5 --- /dev/null +++ b/src/utils/PrivateRoute.jsx @@ -0,0 +1,9 @@ +import React from 'react'; +import { Navigate } from 'react-router-dom'; + +const PrivateRoute = ({ children }) => { + const token = localStorage.getItem('token'); + return token ? children : <Navigate to="/" />; +}; + +export default PrivateRoute; diff --git a/src/utils/api.js b/src/utils/api.js new file mode 100644 index 0000000..7b63aa4 --- /dev/null +++ b/src/utils/api.js @@ -0,0 +1,21 @@ +export const fetchAds = async () => { + const response = await fetch('http://localhost:5000/ads'); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); +}; + +export const addAd = async (ad) => { + const response = await fetch('http://localhost:5000/ads', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(ad), + }); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); +}; |