
โ Setup
1. Firebase ํ๋ก์ ํธ ์์ฑ

2. Firebase ์ค์น
npm install firebase
3. ์ฝ์์์ ์ ๊ณตํด์ฃผ๋ ์ฝ๋๋ฅผ firebase.ts๋ฅผ ์์ฑํด ์์ ๋ฃ์ด์ค๋ค.
โ ์ด๋ฉ์ผ & ๋น๋ฐ๋ฒํธ ๋ก๊ทธ์ธ
Firebase ์ฝ์์ Authentication์์ ์ด๋ฉ์ผ & ๋น๋ฐ๋ฒํธ ์ธ์ฆ์ ํ์ฑํํ ๋ค์, ์ฝ๋์์ initializeํ๋ค.
getAuth(app)
import { getAuth } from "firebase/auth";
export const auth = getAuth(app);
createUserWithEmailAndPassword(auth, email, password)
ํ์ ์ ๋ณด๊ฐ ์กด์ฌํ์ง ์์ ์ ๊ณ์ ์ ์์ฑํ๊ณ ๋ก๊ทธ์ธ ์ํจ๋ค.
UserCredential์ returnํ๋ค. (operationType, providerId, user ๋ฑ์ ์์ฑ์ ๊ฐ๊ณ ์๋ค.)
try {
const credentials = await createUserWithEmailAndPassword(
auth,
email,
password
);
await updateProfile(credentials.user, {
displayName: name,
});
navigate("/");
} catch (error) {
if (error instanceof FirebaseError) {
setError("error", {
message: error.message,
});
}
}
signInWithEmailAndPassword(auth, email, password)
ํ์ ์ ๋ณด๊ฐ ์กด์ฌํ ์ ๋ก๊ทธ์ธ ์ํจ๋ค.
try {
await signInWithEmailAndPassword(auth, email, password);
navigate("/");
} catch (error) {
if (error instanceof FirebaseError) {
setError("error", {
message: error.message,
});
}
}
auth.signOut();
๋ก๊ทธ์์ ์ํจ๋ค.
const onLogOut = async () => {
const ok = confirm("Are you sure you want to log out?");
if (ok) {
await auth.signOut();
navigate("/login");
}
};
โ Github ์์ ๋ก๊ทธ์ธ
Firebase์ console์์ Github ๋ก๊ทธ์ธ์ ํ์ฑํ ์ํจ๋ค.
(Github OAuth ์ค์ ์ ํ๊ณ client ID์ client secret์ ๋ฃ์ด์ค๋ค.)
new GithubAuthProvider()
Github ๋ก๊ทธ์ธ ์ธ์ฆ์ ํ์ํ provider๋ฅผ ์์ฑํ๋ค.
(Github OAuth ๋ก๊ทธ์ธ์ ๊ตฌํํ ๋ ์ฌ์ฉํ๋ ํด๋์ค)
๋ก๊ทธ์ธ์ ํ ๋ 2๊ฐ์ ์ต์ ์ด ์๋ค.
1. signInWithPopup
2. signInWithRedirect
๋ ๋ฉ์๋ ๋ชจ๋ auth์ provider๋ฅผ arg๋ก ๋ฐ๋๋ค.
const onClick = async () => {
try {
const provider = new GithubAuthProvider();
await signInWithPopup(auth, provider);
navigate("/");
} catch (error) {
console.log(error);
}
};
'๐ฅ Firebase' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Firebase - React Native iOS Setup (0) | 2026.02.26 |
|---|---|
| Firebase - Rules (0) | 2025.06.20 |
| Firebase - Hosting (0) | 2025.06.18 |
| Firebase - query(), getDocs(), onSnapshot()์ ์ฌ์ฉํด Firestore DB์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ (0) | 2025.06.16 |
| Firebase - Firestore Database์ Storage์ document์ ์ฌ์ง ์ ๋ก๋ํ๊ธฐ (0) | 2025.06.12 |