From cf39c673da5a2785943109de5c9172d5c607a5ea Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 11 Apr 2024 23:07:32 -0400 Subject: feat: Implement React context for authorization --- src/App.js | 9 +++++++-- src/providers/AuthContext.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/providers/AuthContext.js (limited to 'src') diff --git a/src/App.js b/src/App.js index 069ae93..678f26f 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { BrowserRouter as Router, Routes, Route, useLocation } from 'react-router-dom'; import Navbar from './components/Navbar'; import Footer from './components/Footer'; @@ -13,14 +13,17 @@ import Careers from "./pages/Careers"; import HelpCenter from "./pages/HelpCenter"; import TermsOfService from "./pages/TermsOfService"; import PrivacyPolicy from "./pages/PrivacyPolicy"; +import Dashboard from "./pages/Dashboard"; +import {AuthProvider} from "./providers/AuthContext"; function App() { const location = useLocation(); const excludedRoutes = ['/login', '/sign-up', '/forgot-password']; return ( +
- {!excludedRoutes.includes(location.pathname) && } + {!excludedRoutes.includes(location.pathname) && } } /> } /> @@ -30,12 +33,14 @@ function App() { } /> } /> } /> + } /> } /> } /> {!excludedRoutes.includes(location.pathname) &&
} {!excludedRoutes.includes(location.pathname) && }
+
); } diff --git a/src/providers/AuthContext.js b/src/providers/AuthContext.js new file mode 100644 index 0000000..208b704 --- /dev/null +++ b/src/providers/AuthContext.js @@ -0,0 +1,37 @@ +import React, { createContext, useState } from 'react'; +export const AuthContext = createContext(); + +export const AuthProvider = ({ children }) => { + // actual code we'll use later + // const [user, setUser] = useState(null); + + // test user + const [user, setUser] = useState({ + id: 1, + name: 'John Doe', + email: 'johndoe@gmail.com', + role: 'user', + }); + + const login = (userData) => { + setUser(userData); + }; + + const logout = () => { + setUser(null); + }; + + const authContextValue = { + user, + login, + logout, + }; + + return ( + + {children} + + ); +}; + +export default AuthProvider; -- cgit v1.2.3-70-g09d2