npm install firebase
Firebase๋ฅผ ์ด๊ธฐํํ๊ณ , Firebase ์ฑ ๊ฐ์ฒด๋ฅผ ๋ง๋ ๋ค.
import { initializeApp } from 'firebase/app';
// Replace the following with your app's Firebase project configuration
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTH_DOMAIN,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_APP_ID,
};
// Initialize Firebase
export const firebase = initializeApp(firebaseConfig);
SDK ์ค์น ๋ฐ Firebase ์ด๊ธฐํ - Firebase
Firebase ์ฝ์์ Authentication ์น์
์์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ธ์ฆ ๋ฐฉ๋ฒ์ ํ์ฑํ์ํจ๋ค.
import { initializeApp } from 'firebase/app';
import { getAuth } from 'fbase/auth';
const firebaseConfig = {
...
};
export const firebase = initializeApp(firebaseConfig);
export const auth = getAuth();
import { createUserWithEmailAndPassword, signInWithEmailAndPassword } from 'firebase/auth';
import { auth } from '../fbase';
const Auth = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [newAccount, setNewAccount] = useState(true);
const [error, setError] = useState('');
// input ํธ๋ค๋ฌ
const onChange = (e) => {
const {
target: { name, value },
} = e;
if (name === 'email') {
setEmail(value);
} else {
setPassword(value);
}
};
// ๋ก๊ทธ์ธ
const onSubmit = async (e) => {
e.preventDefault();
try {
let user;
// ์ ๊ท ์ฌ์ฉ์ ๋ฑ๋ก
if (newAccount) {
user = await createUserWithEmailAndPassword(auth, email, password);
// ๊ธฐ์กด ์ฌ์ฉ์ ๋ก๊ทธ์ธ
} else {
user = await signInWithEmailAndPassword(auth, email, password);
}
console.log('user', user); // UserCredentialImpl Object
} catch (error) {
setError(error.message);
}
};
// sign in / create ํ ๊ธ
const toggleAccount = () => setNewAccount((prev) => !prev);
return (
<div>
<form onSubmit={onSubmit}>
<input name='email' type='text' placeholder='Email' required value={email} onChange={onChange} />
<input name='password' type='password' placeholder='Password' required value={password} onChange={onChange} />
<input type='submit' value={newAccount ? 'Create Account' : 'Sign In'} />
{error}
</form>
<span onClick={toggleAccount}>{newAccount ? 'Sign In' : 'Create Account'}</span>
</div>
);
};
Javascript๋ฅผ ์ฌ์ฉํ์ฌ ๋น๋ฐ๋ฒํธ ๊ธฐ๋ฐ ๊ณ์ ์ ์ฌ์ฉํ์ฌ Firebase์ ์ธ์ฆ
์๋ ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ์ ๊ฒฝ์ฐ
๋น๋ฐ๋ฒํธ ํ๋ ธ์ ๊ฒฝ์ฐ
์ด๋ฏธ ๊ฐ์
๋ ์ด๋ฉ์ผ๋ก ํ์๊ฐ์
ํ ๊ฒฝ์ฐ
์ด์ธ์๋ ํ์๊ฐ์ ์ ๋น๋ฐ๋ฒํธ๊ฐ ์งง์ ๊ฒฝ์ฐ ๋ฑ๋ฑ.. ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ๋์์ค๋ค ๐
import { initializeApp } from 'firebase/app';
import { getAuth } from 'fbase/auth';
const firebaseConfig = {
...
};
export const firebase = initializeApp(firebaseConfig);
export const auth = getAuth();
signInWithPopup
์ ํธ์ถํด์ผ ํ๋ค.import { signInWithPopup, GoogleAuthProvider, GithubAuthProvider } from 'firebase/auth';
import { auth } from '../fbase';
const Auth = () => {
const onSocialClick = async (e) => {
const { target: { name } } = e; // const name = e.target.name;
let provider; // Creates the provider object.
if (name === 'google') {
provider = new GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
} else if (name === 'github') {
provider = new GithubAuthProvider();
provider.addScope('repo');
}
const user = await signInWithPopup(auth, provider);
console.log('user', user); // UserCredentialImpl Object
};
return (
<div>
<div>
<button onClick={onSocialClick} name='google'>
Continue with Google
</button>
<button onClick={onSocialClick} name='github'>
Continue with Github
</button>
</div>
</div>
);
};
JavaScript๋ก Google์ ์ฌ์ฉํ์ฌ ์ธ์ฆ - Firebase
JavaScript๋ก GitHub๋ฅผ ์ฌ์ฉํ์ฌ ์ธ์ฆ - Firebase
๊ฐ์
๋ ๊ณ์ ์ Firebase ์ฝ์์์ ์๋์ฒ๋ผ ํ์ธํ ์ ์๋ค.
import { signOut } from 'firebase/auth';
import { auth } from '../fbase';
import { useNavigate } from 'react-router-dom';
const Profile = () => {
const navigate = useNavigate();
const onLogOutClick = () => {
signOut(auth);
navigate('/');
};
return (
<>
My Profile
<button onClick={onLogOutClick}>Log Out</button>
</>
);
};