[React+Firebase] Set up, Auth
์ํ๋ ๋๋ ํฐ๋ฆฌ์
> npx create-react-app ๋ดํ์ ์ด๋ฆ
> code ๋ดํ์ ์ด๋ฆ
์ด๋ ๊ฒ ๋ช ๋ น์ด ์ ๋ ฅํด์ VScode ํธ์ง๊ธฐ๋ก ๋ด ํ์ ์ด๊ธฐ
VScode ํฐ๋ฏธ๋์์ firebase๋ฅผ install ํด์ค๋ค.
npm i firebase@9.6.1
src ํด๋์ firebase.js ํ์ผ ๋ง๋ค์ด์ฃผ๊ณ
๋ด๊ฐ firebase ์ฌ์ดํธ์์ ๋ง๋ ์ฑ ์ ๋ณด ๋ฃ์ด์ฃผ๊ธฐ
import firebase from 'firebase/app';
const firebaseConfig = {
...
};
export default firebase.initializeApp(firebaseConfig);
index.js์์ importํด์ฃผ๊ณ console.log ์ฐ์ด๋ณด๊ธฐ (์คํ์ VScode ํฐ๋ฏธ๋์์ npm run start)
object๊ฐ ์ ๋์จ๋ค.
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
// import ๋ถ๋ถ
import firebase from 'firebase/compat/app';
console.log(firebase);
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
firebase.js ์ key์ url์ ์ ์ด๋๋ ๊ฒ ๋์ ์ .env ํ์ผ์ ์์ฑํ๋ค. (.env ํ์ผ์ srcํด๋ '์'๋ง๊ณ ํด๋์ ๊ฐ์ ๊น์ด์ ์์ด์ผํ๋ค)
.envํ์ผ์๋ ํ๊ฒฝ๋ณ์๊ฐ ๋ค์ด๊ฐ๋๋ฐ React.js ์ดํ๋ฆฌ์ผ์ด์ ์ ๊ฒฝ์ฐ์ ํ๊ฒฝ ๋ณ์๋ฅผ ์จ์ผ ํ๋ค๋ฉด
REACT_APP์ผ๋ก ์์ํด์ผํ๊ณ ๊ทธ ๋ค๋ก ๋ด๊ฐ ์ํ๋ ์ด๋ฆ์ ๋ถ์ฌ์ค์ผํ๋ค.
์์น์ด๋ค!!!
์ด ๋ถ๋ถ์ Github๋ง์ ์ํ ์ฝ๋์ด๋ค. ์๋น์ค๋ฅผ ์ํด ๋น๋ํ๊ณ ์น ์ฌ์ดํธ๋ฅผ ๋์ฐ๋ฉด
create-react-app์ ์ด ์ฝ๋๋ฅผ ๋ชจ๋ ์ค์ ๊ฐ๋ค๋ก ๋ณํํ๋ค.
React Router Setup
src ํด๋์ components, routes ํด๋ ๋ ๊ฐ๋ฅผ ๋ง๋ค์ด์ฃผ์๋ค.
routes์๋ Auth, EditProfile, Home, Profile 4๊ฐ์ ํ์ด์ง๊ฐ ๋ ํ์ผ์ ๋ง๋ค์ด์ฃผ์๋ค.
๊ทธ๋ฆฌ๊ณ components์ Router.js ํ์ผ์ ํ๋ ๋ ๋ง๋ค์ด์ฃผ์๋ค.
๐ฝ Router.js
๋ก๊ทธ์ธ ์ํ๋ฉด Home ์ผ๋ก ์๋๋ผ๋ฉด Auth ํ์ด์ง๋ฅผ ๋ณด์ฌ์ค๋ค.
import React, { useState } from 'react';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import Auth from '../routes/Auth';
import Home from '../routes/Home';
const AppRouter = ({ isLoggedIn }) => {
return (
<Router>
<Switch>
{isLoggedIn ? (
<>
<Route exact path="/">
<Home />
</Route>
</>
) : (
<>
<Route exact path="/">
<Auth />
</Route>
</>
)}
</Switch>
</Router>
);
};
export default AppRouter;
state ๋ App์ผ๋ก ๋ฃ์ด์ฃผ๊ณ props ๋ก ์ ๋ฌํด์คฌ๋ค.
import React, { useState } from 'react';
import AppRouter from './Router';
function App() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
// props ์ ๋ฌ
return <AppRouter isLoggedIn={isLoggedIn} />;
}
export default App;
App ์์ AppRouter๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ footer๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ์๋ ์๊ธฐ ๋๋ฌธ..!
https://firebase.google.com/docs/reference/js/auth
์ธ์ฆ ํจํค์ง | Firebase
Test SDK for Cloud Functions for Firebase
firebase.google.com
auth๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ ์ผ ๋จผ์ ํ ๊ฒ์ import ํ๊ธฐ์ด๋ค.
https://create-react-app.dev/docs/importing-a-component
Importing a Component | Create React App
This project setup supports ES6 modules thanks to webpack.
create-react-app.dev
์ด๋ ๊ฒ ํจ์ผ๋ก์จ ์ ๋๊ฒฝ๋ก๋ก import ํด์ผํ๋ ๊ฒ์ ๊ทธ๋ฅ import ํ ์ ์๊ฒ ๋์๋ค.
(์ปดํจํฐ๊ฐ ๋ชจ๋ ๊ฒ๋ค์ด src๋ก๋ถํฐ ์์๋๋ค๋ ๊ฒ์ ์๊ฒ ๋๋ค.)
- ๋ฒ์ ๋ฌธ์ ๋๋ฌธ์ ์ฝ๋๊ฐ ์กฐ๊ธ์ฉ ๋ฐ๋์๋ค.
// ์ฌ๊ธฐ์์
import fbase from "../fbase";
// ์ด๋ ๊ฒ
import { auth } from 'fbase';
firbase.js ์ด๋ฆ์ fbase.js๋ก ๋ฐ๊พธ๊ณ auth๋ฅผ import ํด์คฌ๋ค
๐ฝ fbase.js
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
const firebaseConfig = {
...
};
const firebaseApp = initializeApp(firebaseConfig);
export const authService = getAuth(firebaseApp);
์ฐ๋ฆฌ๊ฐ ๋ง๋ ์ฑ์์ Authentication ํญ์ ๋ค์ด๊ฐ์ ์ฌ๋ฌ ๊ฐ์ง๋ฅผ ์ค์ ํด์ค๋ค.
์ด๋ฉ์ผ/๋น๋ฐ๋ฒํธ, Google, .Github๋ฅผ ์ผ์คฌ๋๋ฐ
Github๋ฅผ ํค๋ ค๋ฉด Github ์์ App์ ํ๋ ๋ง๋ค์ด์ฃผ์ด์ผํ๋ค.
https://github.com/settings/apps
GitHub: Where the world builds software
GitHub is where over 73 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and feat...
github.com
Register ํด์ค ํ ๋ณด์ด๋ ID์ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
!์ค๋ฅ - ์ด๋ฐ ์ค๋ฅ๊ฐ ๋ฌ๋๋ฐ Network ํญ์์ No throttling์ผ๋ก ์ค์ ํ๋๊น ๋ค์ ์ ๋๋ค.
VM156:6770 crbug/1173575, non-JS module files deprecated.
(anonymous) @ VM156:6770