์ด๋ฉ์ผ๊ณผ ๋น๋ฒ์ ์ฌ์ฉํ๋ firebase.auth์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ๊ณ์ ์ ๋ง๋ค๊ณ ๋ก๊ทธ์ธ ํด๋ณด์!
firebase.auth > Auth์ ๊ฐ๋ฉด ๋ง์ auth provider๋ฅผ ๋ณผ ์ ์๋๋ฐ(facebook provider, google provider, github provider...etc.) EmailAuthProvider(์ด๋ฉ์ผ ์ด์ค ํ๋ก๋ฐ์ด๋)๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฉ์ผ ์ฃผ์์ ํจ์ค์๋๋ก ์๋ก์ด ์ ์ ๊ณ์ ๋ง๋ค๊ณ , ๋ก๊ทธ์ธ์ ํ ์ ์๋ค.
์ฌ์ฉ์๊ฐ ์์์ ์์ฑํ๋ฉด, ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ๊ฑฐ์ณ createUserWithEmailAndPassword ๋ฉ์๋์ ์ ๋ฌํ์ฌ ์๋ก์ด ๊ณ์ ์ ์์ฑํ๋ฉด ๋๋ค.
createUserWithEmailAndPassword()๋ firebase์ Auth ํจํค์ง ์์ ์๋ ๋ฉ์๋๋ก, ์ฌ์ฉ์๊ฐ ์ง์ ํ ์ด๋ฉ์ผ ์ฃผ์ ๋ฐ ์ํธ์ ์ฐ๊ฒฐ๋ ์ ์ฌ์ฉ์ ๊ณ์ ์ ๋ง๋ค ๋ ์ฌ์ฉํ ์ ์๋ค.
์ฌ์ฉ์ ๊ณ์ ์ ์ฑ๊ณต์ ์ผ๋ก ๋ง๋ค๋ฉด ์ด ์ฌ์ฉ์๋ ์ฑ์ ๋ก๊ทธ์ธํ ์ ์๊ณ , ๊ณ์ ์ด ์ด๋ฏธ ์๊ฑฐ๋ ์ํธ๊ฐ ์๋ชป๋ ๊ฒฝ์ฐ์๋ ์ฌ์ฉ์ ๊ณ์ ์ ๋ง๋ค์ง ๋ชปํ ์ ์๋ค.
์ด ๋ฉ์๋๋ promise๋ฅผ ๋ฆฌํดํ๊ธฐ ๋๋ฌธ์ await์ ์ฌ์ฉํ๋ฉด ๋๋ค.
v.9์์ createUserWithEmailAndPassword()๋ฉ์๋ ์ฌ์ฉํด ์๋ก์ด ๊ณ์ ๋ง๋ค๊ธฐ
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth"; // const auth = getAuth(); createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => { // Signed in const user = userCredential.user; // ... }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; // .. });
signInWithEmailAndPassword()๋ firebase์ Auth ํจํค์ง ์์ ์๋ ๋ฉ์๋๋ก, ์ด๋ฉ์ผ ๋ฐ ์ํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋น๋๊ธฐ์ ์ผ๋ก ๋ก๊ทธ์ธํ ์ ์๋ค.
v.9์์ signInWithEmailAndPassword()๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๋ก๊ทธ์ธ ํ๊ธฐ
import { getAuth, signInWithEmailAndPassword } from "firebase/auth"; // const auth = getAuth(); signInWithEmailAndPassword(auth, email, password) .then((userCredential) => { // Signed in const user = userCredential.user; // ... }) .catch((error) => { const errorCode = error.code; const errorMessage = error.message; });
const Auth = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
//๐ฅ ์๋ก์ด ๊ณ์ ๋ง๋ค๊ธฐ
const [newAccount, setNewAccount] = useState(true);
//์๋ต
{/*๐ฅ ๊ณ์ ์๋ก ๋ง๋ค์ด์ผ ํ๋ฉด ๋ก๊ทธ์ธ ๋ฒํผ ๋์ ๊ณ์ ์๋ก ๋ง๋๋ ๋ฒํผ ๋ณด์ฌ์ฃผ๊ธฐ*/}
<input type="submit" value={newAccount ? "Create Account" : "Login"} />
</form>
//๐ฅ ์ด๋ฉ์ผ, ๋น๋ฒ์ผ๋ก ์๋ก์ด ๊ณ์ ์์ฑ ๋ฐ ๋ก๊ทธ์ธ ํ๊ธฐ์ํด ์๋ ๋ฉ์๋ ์ํฌํธํ๊ธฐ
import {
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
} from "firebase/auth";
//๐ฅ getAuth()๋ฅผ auth๋ก ๋ช
๋ช
const auth = getAuth();
newAccount์ state๋ก newAccount๊ฐ ์ฐธ์ด๋ฉด ์๋ก์ด ๊ณ์ ์ ๋ง๋๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ , ๊ฑฐ์ง์ด๋ฉด ๋ก๊ทธ์ธํ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์.
//form์ ์ฌ์ฉํ๋ ์ด๋ฒคํธ
//๐ฅ createUserWithEmailAndPassword() ๋ฉ์๋์ signInWithEmailAndPassword()๋ฉ์๋๊ฐ promise๋ฅผ ๋ฆฌํดํ๊ธฐ ๋๋ฌธ์ async await์ ์ฌ์ฉํ์!
const onSubmit = async (event) => {
event.preventDefault();
//๐ฅ form submitํ๋ฉด newAccount ์คํ
์ดํธ๋ก ํ์ธํ์!
//๐ฅ newAccount๊ฐ ์ฐธ์ด๋ฉด ๊ณ์ ์๋ก ๋ง๋ค๊ณ , ๊ฑฐ์ง์ด๋ฉด ๋ก๊ทธ์ธํ๊ธฐ
//๐ฅ๐ฅ ๊ทธ๋ฆฌ๊ณ ํธ๋ผ์ด ์บฃ์น ๊ตฌ๋ฌธ ์ฌ์ฉํด์ ํน์ ๋ชจ๋ฅผ ์ค๋ฅ๋ฅผ ๋๋นํ์.
try {
//๐ฅ ์ป๊ฒ๋๋ dataํ์ธ ์ํด data๋ผ๋ ๋ณ์๋ฅผ ์ ํ๊ณ newAccount๊ฐ ์ฐธ์ผ ๋ data์ ๊ณ์ ์์ฑํ๋ ๋ฉ์๋ ๋ฃ๊ณ ,
//newAccount๊ฐ ๊ฑฐ์ง์ผ ๋ data์ ๋ก๊ทธ์ธํ๋ ๋ฉ์๋ ๋ฃ์.
let data;
if (newAccount) {
//๐ฅ ํ๋ผ๋ฏธํฐ๋ก auth, email, password๋ฅผ ๋ณด๋ด๋ฉด ๋๋ค.
data = await createUserWithEmailAndPassword(auth, email, password);
} else {
data = await signInWithEmailAndPassword(auth, email, password);
}
console.log(data);
} catch (error) {
console.log(error);
}
};
//๐ฅ ์ด๋ฉ์ผ, ๋น๋ฒ์ผ๋ก ์๋ก์ด ๊ณ์ ์์ฑ ๋ฐ ๋ก๊ทธ์ธ ํ๊ธฐ์ํด ์๋ ๋ฉ์๋ ์ํฌํธํ๊ธฐ
//๐ฅ getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword
import {
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
} from "firebase/auth";
import { useState } from "react";
const Auth = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
//๐ฅ ์๋ก์ด ๊ณ์ ๋ง๋ค๊ธฐ
const [newAccount, setNewAccount] = useState(true);
//๐ฅ getAuth()๋ฅผ auth๋ก ๋ช
๋ช
const auth = getAuth();
//email, password์ ์ฌ์ฉํ๋ ์ด๋ฒคํธ
const onChange = (event) => {
const {
target: { name, value },
} = event;
if (name === "email") {
setEmail(value);
} else if (name === "password") {
setPassword(value);
}
};
//form์ ์ฌ์ฉํ๋ ์ด๋ฒคํธ
//c๐ฅ reateUserWithEmailAndPassword() ๋ฉ์๋์ signInWithEmailAndPassword()๋ฉ์๋๊ฐ promise๋ฅผ ๋ฆฌํดํ๊ธฐ ๋๋ฌธ์ async await์ ์ฌ์ฉํ์!
const onSubmit = async (event) => {
event.preventDefault();
//๐ฅ form submitํ๋ฉด newAccount ์คํ
์ดํธ๋ก ํ์ธํ์!
//๐ฅ newAccount๊ฐ ์ฐธ์ด๋ฉด ๊ณ์ ์๋ก ๋ง๋ค๊ณ , ๊ฑฐ์ง์ด๋ฉด ๋ก๊ทธ์ธํ๊ธฐ
//๐ฅ๐ฅ ๊ทธ๋ฆฌ๊ณ ํธ๋ผ์ด ์บฃ์น ๊ตฌ๋ฌธ ์ฌ์ฉํด์ ํน์ ๋ชจ๋ฅผ ์ค๋ฅ๋ฅผ ๋๋นํ์.
try {
//๐ฅ ์ป๊ฒ๋๋ dataํ์ธ ์ํด data๋ผ๋ ๋ณ์๋ฅผ ์ ํ๊ณ newAccount๊ฐ ์ฐธ์ผ ๋ data์ ๊ณ์ ์์ฑํ๋ ๋ฉ์๋ ๋ฃ๊ณ ,
//newAccount๊ฐ ๊ฑฐ์ง์ผ ๋ data์ ๋ก๊ทธ์ธํ๋ ๋ฉ์๋ ๋ฃ์.
let data;
if (newAccount) {
//๐ฅ ํ๋ผ๋ฏธํฐ๋ก auth, email, password๋ฅผ ๋ณด๋ด๋ฉด ๋๋ค.
data = await createUserWithEmailAndPassword(auth, email, password);
} else {
data = await signInWithEmailAndPassword(auth, email, password);
}
console.log(data);
} catch (error) {
console.log(error);
}
};
return (
<div>
<h1>๋ก๊ทธ์ธ ํ์ด์ง</h1>
<p>๋ก๊ณ ๋ฃ๊ธฐ</p>
<form onSubmit={onSubmit}>
<input
type="email"
placeholder="Email"
required
value={email}
name="email"
onChange={onChange}
></input>
<input
type="password"
placeholder="Password"
required
value={password}
name="password"
onChange={onChange}
></input>
{/*๐ฅ ๊ณ์ ์๋ก ๋ง๋ค์ด์ผ ํ๋ฉด ๋ก๊ทธ์ธ ๋ฒํผ ๋์ ๊ณ์ ์๋ก ๋ง๋๋ ๋ฒํผ ๋ณด์ฌ์ฃผ๊ธฐ*/}
<input type="submit" value={newAccount ? "Create Account" : "Login"} />
</form>
<div>
<button>Continue with Google</button>
<button>Continue with GitHub</button>
</div>
</div>
);
};
export default Auth;


์ ํ~! ^3^~
์๋ก์ด ๊ณ์ ์ด ๋ง๋ค์ด ์ก๊ณ ๋ก๊ทธ์ธ๋ ๋์ด ์๋ค.
setPersistence() ๋ฉ์๋๋ ์ฌ์ฉ์๋ค์ ์ด๋ป๊ฒ ๊ธฐ์ตํ ๊ฒ์ธ์ง ์ ํํ ์ ์๊ฒํด์ค๋ค.
ํ์ฌ ์ ์ฅ๋ Auth ์ธ์ ์ Auth ์ธ์คํด์ค(์ฌ๋ก)์์ ์ง์์ฑ ์ ํ์ ๋ณ๊ฒฝํ๊ณ ๋ฆฌ๋ค์ด๋ ํธ ์์ฒญ์ ์ฌ์ฉํ ์ฌ์ธ์ธ์ ํฌํจํ์ฌ ํฅํ ๋ก๊ทธ์ธ ์์ฒญ์ ์ด ์ ํ์ ์ง์์ฑ์ ์ ์ฉํ๋ค.
์ด๊ฒ์ ์ํด, ์ ์ ๋ ์๋ช ํ๊ณ ์๋ ์ ์ ์ ์ธ์ ์ ๊ธฐ์ต ์ฌ๋ถ๋ฅผ ๊ฐ๋จํ๊ฒ ์ง์ ํ ์ ์๊ณ , ๋ค๋ฅธ ์ ์ ๊ฐ ๊ณต์ ํ๊ณ ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ด๋ ๊ธฐ๋ฐ ๋ฐ์ดํฐ๊ฐ ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ Auth ์ํ๋ฅผ ์ ์งํ์ง ์๊ฒ ๋๋ค.
React Native ์ฑ๊ณผ ์น ๋ธ๋ผ์ฐ์ ์์์ ์ด๊ธฐ ๊ฐ์ local์ด๋ค.
value ์ค๋ช local๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ๋๋ผ๋ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ธฐ์ต sessionํญ(tab)์ด ์ด๋ ค์๋ ๋์์๋ง ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ธฐ์ต none์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ธฐ์ตํ์ง ์์(๋ก๊ทธ์ธ์ ๋์ง๋ง ํ์ด์ง ์๊ณ ํ๋ฉด ๋ก๊ทธ์์๋์ ๋ค์ ๋ก๊ทธ์ธํด์ผ ํจ)
Inspection > Application > IndexedDB > firebaseLocalStorageDb๋ก ๊ฐ๋ฉด ์ฌ์ฉ์ ์ ๋ณด๊ฐ ๋ณด์ธ๋ค.

์๋ก๊ณ ์นจ์ ํด๋ ์ฌ์ฉ์ ์ ๋ณด๊ฐ ์ฌ์ ํ ๋จ์ ์๋ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ์ฆ, firebase๊ฐ ์ ์ํ ์ ์ ๋ฅผ ๊ธฐ์ตํ๊ณ ์๋ค. ์ค์ผ์ด ใ ใ !