ํ์ ๊ธฐ๋ฐ OAuth ์ธ์ฆ ํ๋ฆ์ ์ฌ์ฉํ์ฌ Firebase ํด๋ผ์ด์ธํธ๋ฅผ ์ธ์ฆํ๋ค. ์ฆ, ํ์ ์ฐฝ ์ด์ด ๋ก๊ทธ์ธ ์ธ์ฆ์ ํ๋ ๋ฉ์๋์ด๋ค.
signInWithRedirect()๋ฅผ ์ฌ์ฉํ๋ฉด ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋ฆฌ๋๋ ์
ํ์ฌ ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธํ๋๋ก ์์ฒญํ๋ค. ๋ฆฌ๋๋ ์
๋ฐฉ๋ฒ์ ๋ชจ๋ฐ์ผ ์ฅ์น์์ ์ ํธ๋๋ค.export declare function signInWithPopup(auth: Auth, provider: AuthProvider, resolver?: PopupRedirectResolver): Promise<UserCredential>;
| Parameter | Type | Description |
|---|---|---|
| auth | Auth | The Auth instance. |
| provider | AuthProvider | ์ธ์ฆํ ๊ณต๊ธ์. ๊ณต๊ธ์๋ OAuthProvider์ฌ์ผ ํ๋ค. EmailAuthProvider์ ๊ฐ์ OAuth๊ฐ ์๋ ๊ณต๊ธ์๋ ์ค๋ฅ๋ฅผ ๋ฐ์์ํจ๋ค. |
| resolver | PopupRedirectResolver | PopupRedirectResolver์ ์ธ์คํด์ค. ์ด๋ฏธ initializeAuth()์ ์ ๊ณต๋๊ฑฐ๋ getAuth()์ ์ํด ์ ๊ณต๋ ๊ฒฝ์ฐ์๋ ์ต์
๋ํ๋ค. |
signInWithPopup()๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ์ฐฝ์ ์ด์ด ์ฌ์ฉ์์๊ฒ Google ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ๋๋ก ์์ฒญํ๊ธฐimport { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth"; // const auth = getAuth(); signInWithPopup(auth, provider) .then((result) => { // This gives you a Google Access Token. You can use it to access the Google API. const credential = GoogleAuthProvider.credentialFromResult(result); const token = credential.accessToken; // The signed-in user info. const user = result.user; // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.email; // The AuthCredential type that was used. const credential = GoogleAuthProvider.credentialFromError(error); // ... });
signInWithPopup()๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ์ฐฝ์ ์ด์ด ์ฌ์ฉ์์๊ฒ GitHub ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ๋๋ก ์์ฒญํ๊ธฐimport { getAuth, signInWithPopup, GithubAuthProvider } from "firebase/auth"; // const auth = getAuth(); signInWithPopup(auth, provider) .then((result) => { // This gives you a GitHub Access Token. You can use it to access the GitHub API. const credential = GithubAuthProvider.credentialFromResult(result); const token = credential.accessToken; // The signed-in user info. const user = result.user; // ... }).catch((error) => { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; // The email of the user's account used. const email = error.email; // The AuthCredential type that was used. const credential = GithubAuthProvider.credentialFromError(error); // ... });
๋ณด์ํ๋ ๊ตฌ๊ธ/๊นํ ํ๋ก๋ฐ์ด๋ ๋ฐฉ์ ๋ ๋ค ๋๊ฐ๋ค.

<div>
<button name="google">Google ๊ณ์ ์ผ๋ก ๊ณ์ํ๊ธฐ</button>
<button name="gitHub">GitHub ๊ณ์ ์ผ๋ก ๊ณ์ํ๊ธฐ</button>
</div>
const onSocialClick = (event) => {
console.log(event.target.name);
};
<div>
<button name="google" onClick={onSocialClick}>
Google ๊ณ์ ์ผ๋ก ๊ณ์ํ๊ธฐ
</button>
<button name="gitHub" onClick={onSocialClick}>
GitHub ๊ณ์ ์ผ๋ก ๊ณ์ํ๊ธฐ
</button>
</div>
import { useState } from "react";
//์ด๋ฉ์ผ, ๋น๋ฒ์ผ๋ก ์๋ก์ด ๊ณ์ ์์ฑ ๋ฐ ๋ก๊ทธ์ธ ํ๊ธฐ์ํด ์๋ ๋ฉ์๋ ์ํฌํธํ๊ธฐ
//getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword
import {
getAuth,
createUserWithEmailAndPassword,
signInWithEmailAndPassword,
GoogleAuthProvider,
GithubAuthProvider,
signInWithPopup,
} from "firebase/auth";
const Auth = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [newAccount, setNewAccount] = useState(true);
//error ์คํ
์ด๋๋ฅผ ๋ง๋ค์. ๋ํดํธ ๊ฐ์ผ๋ก ๋น์ด ์๋ ํ
์คํธ ์ฃผ๊ธฐ
const [error, setError] = useState("");
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์ ์ฌ์ฉํ๋ ์ด๋ฒคํธ
const onSubmit = async (event) => {
event.preventDefault();
//form submitํ๋ฉด newAccount ์คํ
์ดํธ๋ก ํ์ธํ์!
//newAccount๊ฐ ์ฐธ์ด๋ฉด ๊ณ์ ์๋ก ๋ง๋ค๊ณ , ๊ฑฐ์ง์ด๋ฉด ๋ก๊ทธ์ธํ๊ธฐ
try {
if (newAccount) {
await createUserWithEmailAndPassword(auth, email, password);
setNewAccount(false);
} else {
await signInWithEmailAndPassword(auth, email, password);
}
} catch (error) {
//์๋ฌ๊ฐ ์๊ธฐ๋ฉด error ์คํ
์ดํธ์ ๋ฃ์ด์ ์๋ฌ ๋ฉ์ธ์ง ๋์ฐ๊ธฐ
//console.log(error.message);
setError(error.message);
}
};
//๊ฐ์
ํด์ผํ๋์ง ๋ก๊ทธ์ธํด์ผ ํ๋์ง ํ ๊ธํ๋ ํจ์: newAccount์ ์ด์ ๊ฐ๊ณผ ๋ฐ๋๋๋ ๊ฐ ๋ฆฌํดํ๊ธฐ
const toggleAccount = () => setNewAccount((prev) => !prev);
//๐ฅ๐ฅ์์
๋ก๊ทธ์ธ ๋ฒํผ ํด๋ฆญํ๋ฉด ์คํ๋ ํจ์ ์์ฑ
const onSocialClick = async (event) => {
//console.log(event.target.name);
const {
target: { name },
} = event;
let provider;
//๐ฅ๐ฅํ๊ฒ์ name์ ๋ฐ๋ผ(์ฆ, ๋ฌด์จ ๋ฒํผ์ด ๋๋ฆฌ๋์ง์ ๋ฐ๋ผ)
if (name === "google") {
//๐ฅprovider ๊ตฌ๊ธ๋ก ์ค์
provider = new GoogleAuthProvider();
} else if (name === "github") {
//๐ฅprovider ๊นํ์ผ๋ก ์ค์
provider = new GithubAuthProvider();
}
await signInWithPopup(auth, provider);
};
return (
<div>
<h1>๋ก๊ทธ์ธ ํ์ด์ง</h1>
<p>๋ก๊ณ ๋ฃ๊ธฐ</p>
<form onSubmit={onSubmit}>
<input
type="email"
placeholder="์ด๋ฉ์ผ"
required
value={email}
name="email"
onChange={onChange}
></input>
<input
type="password"
placeholder="๋น๋ฐ๋ฒํธ"
required
value={password}
name="password"
onChange={onChange}
></input>
<input type="submit" value={newAccount ? "๊ฐ์
ํ๊ธฐ" : "๋ก๊ทธ์ธ"} />
{/*error ๋ณด์ฌ์ฃผ๊ธฐ*/}
{error}
</form>
{/*newAccount(์๋ก์ด ๊ณ์ )๊ฐ ์ฐธ์ด๋ฉด ๋ก๊ทธ์ธ, ๊ฑฐ์ง์ด๋ฉด ๊ฐ์
ํ๊ธฐ๊ฐ ๋๋ ํ ๊ธ ๋ง๋ค๊ธฐ */}
<button onClick={toggleAccount}>
{newAccount ? "๋ก๊ทธ์ธ" : "๊ฐ์
ํ๊ธฐ"}
</button>
<div>
{/*๐ฅ ๋ฒํผ ๋ง๋ค๊ณ onClick ์ด๋ฒคํธ์ onSocialClick ํจ์ ์ฐ๊ฒฐ*/}
<button name="google" onClick={onSocialClick}>
Google ๊ณ์ ์ผ๋ก ๊ณ์ํ๊ธฐ
</button>
<button name="github" onClick={onSocialClick}>
GitHub ๊ณ์ ์ผ๋ก ๊ณ์ํ๊ธฐ
</button>
</div>
</div>
);
};
export default Auth;