App.js
import './App.css';
import { Outlet } from 'react-router-dom';
import Navbar from './components/Navbar';
function App() {
return (
<>
<Navbar />
<Outlet />
</>
);
}
export default App;
index.css
@tailwind base;
@tailwind components;
@tailwind utilities;
@import url("https://fonts.googleapis.com/css2?family=Noto+Sans+KR&family=Playfair+Display&display=swap");
body {
margin: 0;
font-family: Noto Sans KR;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
@apply text-slate-900;
}
#root {
@apply w-full text-inherit;
}
index.js
import React from "react";
import ReactDOM from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
import "./index.css";
import App from "./App";
import NotFound from "./pages/NotFound";
import Home from "./pages/Home";
import AllProducts from "./pages/AllProducts";
import NewProduct from "./pages/NewProduct";
import ProductDetail from "./pages/ProductDetail";
import MyCart from "./pages/MyCart";
const router = createBrowserRouter([
{
path: "/",
element: <App />,
errorElement: <NotFound />,
children: [
{ index: true, path: "/", element: <Home /> },
{ path: "/products", element: <AllProducts /> },
{ path: "/products/new", element: <NewProduct /> },
{ path: "/products/:id", element: <ProductDetail /> },
{ path: "/cart", element: <MyCart /> },
],
},
]);
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
Navbar.jsx
import React, { useState } from "react";
import { Link } from "react-router-dom";
import { HiPencilAlt } from "react-icons/hi";
import { login, logout } from "../api/firebase";
export default function Navbar() {
const [user, setUser] = useState();
// onClick์ login ํจ์๋ฅผ ๋ฃ์ง ์๊ณ ์ด๋ ๊ฒ ์์ฑํ๋ ์ด์ ๋ firebase.js์ ์๋ user๋ฅผ ๋ฐ์์์ useState์ ์ง์ด๋ฃ๊ธฐ ์ํจ
/**
* ๋ก๊ทธ์ธํ ๋ ์ฌ์ฉ๋๋ ํจ์
*/
const handleLogin = () => {
login().then(setUser);
};
const handleLogout = () => {
logout().then(setUser); // useState์ user๋ฅผ ๋น์ด๋ค. (null ์ํ๋ก ๋ง๋ฆ)
};
return (
<div className="border-b border-t-slate-300">
<div className="w-full max-w-screen-2xl m-auto">
<header className="flex justify-between items-center p-5">
<h1 className="text-3xl font-logoFont tracking-widest">
RALPH<span className="pl-3 md:pl-6">LAUREN</span>
</h1>
<nav className="flex items-center gap-4">
<Link to="/products">Product</Link>
<Link to="/cart">Cart</Link>
<Link to="/products/new">
<HiPencilAlt />
</Link>
{!user && <button onClick={handleLogin}>login</button>}
{user && <button onClick={handleLogout}>logout</button>}
</nav>
</header>
</div>
</div>
);
}
firebase.js
import { initializeApp } from "firebase/app";
import { getAuth, signInWithPopup, GoogleAuthProvider, signOut } from "firebase/auth";
const firebaseConfig = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
databaseURL: process.env.REACT_APP_FIREBASE_DB_URL,
};
const app = initializeApp(firebaseConfig);
const provider = new GoogleAuthProvider();
const auth = getAuth();
// * ํจ์ ์ ์ธ์ firebase ์์์ ํจ
export function login() {
return signInWithPopup(auth, provider) // ์๋ ๋ฆฌํด๋ user ๊ฐ์ ๋ฐ์์์ ๊ฒฐ๊ณผ๊ฐ์ผ๋ก ๋ด๋ณด๋ด๊ธฐ ์ํด return ์์ฑ
.then((result) => {
// ๋ก๊ทธ์ธ ๋์๋์ง ๊ฒฐ๊ณผ๋ฅผ ์ป์ด์ด
const user = result.user;
// console.log("user? : ", user);
return user.displayName;
})
.catch(console.error);
}
export function logout() {
return signOut(auth) // null๊ฐ์ด ๋ฆฌํด๋จ
.then(() => {}) // null
// .catch((error) => {});
}
js
์ด๋ฏธ์ง๋ก ๋์ฒด
์ด๋ฏธ ๋ก๊ทธ์ธ ๋์ด ์๊ธฐ ๋๋ฌธ์ ์๋์ ๊ฐ์ด ๋ก๊ทธ์ธ ์ฐฝ์ด ๋จ์ง ์๊ณ ๋ฐ๋ก ๋ก๊ทธ์์์ผ๋ก ๋์ด๊ฐ