๐Ÿ”ฅ Firebase Authentication

์ง€์€ยท2023๋…„ 2์›” 1์ผ
0

๐Ÿš‚ ํ† ์ด ํ”„๋กœ์ ํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
2/10
post-thumbnail

1. Firebase ์„ค์น˜

npm install firebase

2. Firebase ์„ค์ •ํ•˜๊ธฐ

Firebase๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , Firebase ์•ฑ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

๐Ÿ“‚ fbase.js

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


3. Authentication

Firebase ์ฝ˜์†”์˜ Authentication ์„น์…˜์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ธ์ฆ ๋ฐฉ๋ฒ•์„ ํ™œ์„ฑํ™”์‹œํ‚จ๋‹ค.

์ด๋ฉ”์ผ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ๋กœ๊ทธ์ธ

๐Ÿ“‚ fbase.js

import { initializeApp } from 'firebase/app';
import { getAuth } from 'fbase/auth';

const firebaseConfig = {
  ...
};
  
export const firebase = initializeApp(firebaseConfig);
export const auth = getAuth();

๐Ÿ“‚ Auth.js

  • ๊ธฐ์กด ์‚ฌ์šฉ์ž๋Š” Sign In, ์‹ ๊ทœ ๊ฐ€์ž…์ž๋Š” Create Account๋กœ ๊ฐ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.
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์— ์ธ์ฆ

  • ์—†๋Š” ๊ณ„์ •์œผ๋กœ ๋กœ๊ทธ์ธํ–ˆ์„ ๊ฒฝ์šฐ

  • ๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ ธ์„ ๊ฒฝ์šฐ

  • ์ด๋ฏธ ๊ฐ€์ž…๋œ ์ด๋ฉ”์ผ๋กœ ํšŒ์›๊ฐ€์ž…ํ•  ๊ฒฝ์šฐ

์ด์™ธ์—๋„ ํšŒ์›๊ฐ€์ž… ์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์งง์€ ๊ฒฝ์šฐ ๋“ฑ๋“ฑ.. ์—๋Ÿฌ ๋ฉ”์„ธ์ง€๋ฅผ ๋„์›Œ์ค€๋‹ค ๐Ÿ‘


Google, GitHub์œผ๋กœ ๋กœ๊ทธ์ธ

๐Ÿ“‚ fbase.js

import { initializeApp } from 'firebase/app';
import { getAuth } from 'fbase/auth';

const firebaseConfig = {
 ...
};

export const firebase = initializeApp(firebaseConfig);
export const auth = getAuth();

๐Ÿ“‚ Auth.js

  • ํŒ์—… ์ฐฝ์œผ๋กœ ๋กœ๊ทธ์ธํ•˜๋ ค๋ฉด 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 ์ฝ˜์†”์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.


+ ๋กœ๊ทธ์•„์›ƒํ•˜๋Š” ๋ฒ•

๐Ÿ“‚ Profile.js

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>
		</>
	);
};
profile
๋ธ”๋กœ๊ทธ ์ด์ „ -> https://janechun.tistory.com

0๊ฐœ์˜ ๋Œ“๊ธ€