diff --git a/social media react.js b/social media react.js new file mode 100644 index 00000000..1e108558 --- /dev/null +++ b/social media react.js @@ -0,0 +1,99 @@ +// App.jsx +import React from "react"; +import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; +import { AuthProvider } from "./context/AuthContext"; +import Navbar from "./components/Navbar"; +import Home from "./pages/Home"; +import Login from "./pages/Login"; +import Register from "./pages/Register"; +import Profile from "./pages/Profile"; + +const App = () => { + return ( + + + + + } /> + } /> + } /> + } /> + + + + ); +}; + +export default App; + +// firebase.js +import { initializeApp } from "firebase/app"; +import { getAuth } from "firebase/auth"; +import { getFirestore } from "firebase/firestore"; +import { getStorage } from "firebase/storage"; + +const firebaseConfig = { + apiKey: "YOUR_API_KEY", + authDomain: "YOUR_AUTH_DOMAIN", + projectId: "YOUR_PROJECT_ID", + storageBucket: "YOUR_STORAGE_BUCKET", + messagingSenderId: "YOUR_MESSAGING_SENDER_ID", + appId: "YOUR_APP_ID", +}; + +const app = initializeApp(firebaseConfig); +export const auth = getAuth(app); +export const db = getFirestore(app); +export const storage = getStorage(app); + +// context/AuthContext.jsx +import React, { createContext, useContext, useEffect, useState } from "react"; +import { onAuthStateChanged } from "firebase/auth"; +import { auth } from "../utils/firebase"; + +const AuthContext = createContext(); + +export const AuthProvider = ({ children }) => { + const [user, setUser] = useState(null); + + useEffect(() => { + const unsubscribe = onAuthStateChanged(auth, (currentUser) => { + setUser(currentUser); + }); + return () => unsubscribe(); + }, []); + + return ( + + {children} + + ); +}; + +export const useAuth = () => useContext(AuthContext); + +// components/Navbar.jsx +import React from "react"; +import { Link } from "react-router-dom"; +import { useAuth } from "../context/AuthContext"; + +const Navbar = () => { + const { user } = useAuth(); + return ( + + ); +}; + +export default Navbar;