์ค๋ ์๋ฃํ ๋ชฉ๋ก
1. ๋ก๊ทธ์ธ ๊ตฌํ๊ธฐ๋ฅ
2. ์์
๋ก๊ทธ์ธ (Github, Google)
3. ๋ก๊ทธ์์ ๊ธฐ๋ฅ
๋ฐฑ์๋ ํํธ๋ฅผ ํ์ด์ด๋ฒ ์ด์ค๋ฅผ ํ์ฉํ๊ธฐ ๋๋ฌธ์ ํ์ด์ด๋ฒ ์ด์ค ์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํ๋ค.
https://firebase.google.com/docs/auth/web/firebaseui?hl=ko
์ํ ์ ๋ฐ์ดํธ๋ฅผ ์ํด useState ํจ์๋ฅผ ์ด์ฉํ๋ค.
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [newAccount, setnewAccount] = useState(true);
const [error, setError] = useState("");
ํ์ด์ด๋ฒ ์ด์ค์์ ๋ก๊ทธ์ธ์ ์ง์ํ๊ธฐ ์ํด์๋ setPersistence๋ฅผ ์ฌ์ฉํ๋ค.
setPersistence๋ local, session, none์ต์
์ ๊ฐ๊ณ ์๋ค.
local : ์น ๋ธ๋ผ์ฐ์ ๋ฅผ ์ข
๋ฃํด๋ ๋ก๊ทธ์ธ ์ ์ง
Local ์ต์
์ ์น ๋ธ๋ผ์ฐ์ ๋ฅผ ์ข
๋ฃํด๋ ๋ก๊ทธ์ธํ ์ฌ์ฉ์์ ์ ๋ณด๋ฅผ ๊ธฐ์ตํ๊ฒ ํด์ค๋ค.
session : ์น ๋ธ๋ผ์ฐ์ ์ ํญ์ ์ข ๋ฃํ๋ฉด ๋ก๊ทธ์์
none : ์๋ก๊ณ ์นจํ๋ฉด ๋ก๊ทธ์์
ํ์ง๋ง ๊ธฐ๋ณธ๊ฐ์ local๋ก ๋์ด ์์ผ๋ฏ๋ก ๋ฐ๋ก ์ค์ ํ ํ์์์ด ์งํํ๋ค.
<div>
<button name="Google" onClick ={onSocialClick} >Continue with Google</button>
<button name="Github" onClick ={onSocialClick} >Continue with Github</button>
</div>
const onSocialClick = async (event) =>{
const {target:{name},
} = event;
let provider;
if(name === "Google"){
provider = new GoogleAuthProvider();
}else if(name === "Github"){
provider = new GoogleAuthProvider();
}
await signInWithPopup(auth, provider);
};
๋จผ์ ๊ตฌ๊ธ๊ณผ ๊นํ๋ธ ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ๊ธฐ ์ํด ํ์ด์ด๋ฒ ์ด์ค์์๋
GoogleAuthProvider์ GithubAuthProvider๋ฅผ ์ด์ฉํ๋ค.
๋ํ signInWithPopupํจ์๊ฐ ๋น๋๊ธฐ ์์
์ด๊ธฐ ๋๋ฌธ์ async-await๋ฌธ์ ์ฌ์ฉํ๋ค.
provider์ signInWithPopup ํจ์ ์ธ์๋ก ๋๊ฒจ ๋ก๊ทธ์ธ์ ์ฌ์ฉํ๋ค.
๋ก๊ทธ์์ ๊ธฐ๋ฅ์ ๋ฒํผ์ ์ด์ฉํ์ฌ ํ๊ธฐ ์ํด ๋ค๋น๊ฒ์ด์ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์๋ค.
const Navigation = () =>
<nav>
<ul>
<li>
<Link to ="/">Home</Link>
</li>
<li>
<Link to ="/profile">Profile</Link>
</li>
</ul>
</nav>
Profile์ ํด๋ฆญํ๋ฉด /profile๋ก ์ด๋ํ๊ฒ ๋ผ์ฐํฐ์ ์ถ๊ฐํ๋ค.
const Profile = () => <span>Profile</span>
export default () => {
const onLogOutClick = () => auth.signOut();
return (
<>
<button onClick ={onLogOutClick}>Log out</button>
</>
);
};
import { auth } from 'fbase';
const Profile = () => <span>Profile</span>
export default () => {
const onLogOutClick = () => auth.signOut();
return (
<>
<button onClick ={onLogOutClick}>Log out</button>
</>
);
};
ํ์ด์ด๋ฒ ์ด์ค์์ ๋ก๊ทธ์์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ signOut()ํจ์๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ onLogoutClick ํจ์๋ฅผ ๋ง๋ค๊ณ , ๋ฒํผ์ ํด๋ฆญํ๋ฉด signOutํจ์๊ฐ ๋ฐ๋๋๊ฒ ์ฝ๋๋ฅผ ์งฐ๋ค.
ํ์ง๋ง ๋ก๊ทธ์์์ ํด๋ ์ฌ์ ํ Profile ์ฌ์ดํธ์ ์์น ํ๊ธฐ ๋๋ฌธ์,
Redirect๋ฅผ ๋ฐฐ์นํ๋ค.
const AppRouter = ({isLoggedIn}) =>{ //Router์ ์ด๋ฏธ ์ ์ํ๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ์ ์
//์์ ์ปดํฌ๋ํธ์์ ๋ฐ์ ํ๋กญ์ค๋ ๊ตฌ์กฐ๋ถํด ํ ๋น์ผ๋ก ์ฌ์ฉ
return( // switch๋ฅผ ์ด์ฉํ๋ฉด ์ฌ๋ฌ๊ฐ์ง ๋ผ์ฐํธ ์ค ํ๋๋ง ๋ ๋๋งํ๊ฒ ํด์ค.
<Router>
{isLoggedIn &&<Navigation />}
{/* && -> ๋ก๊ทธ์ธ์ด ๋ง๋ค๋ฉด Navigation => true*/}
<Switch>
{isLoggedIn ?( // ๋ก๊ทธ์ธ ์ํ ์
<>
<Route exact path ="/">
<Home />
</Route>
<Route exact path ="/profile">
<Profile />
</Route>
<Redirect from="*" to="/" />
</>
) : ( // ๋น๋ก๊ทธ์ธ ์ํ ์
<>
<Route exact path ="/">
<Auth />
</Route>
<Redirect from="*" to="/" />
</>
)}
</Switch>
</Router>
)
}
์์ธํ ์ค๋ช
ํ์๋ฉด switch๋ฅผ ์ด์ฉํ์ฌ ํ ๊ฐ์ง ๋ผ์ฐํธ๋ง ๋ ๋๋งํ๊ฒ ํด์ฃผ๋๋ฐ,
isLoggedIn์ ์ํ๋ฅผ ๋ณด๊ณ ๋ก๊ทธ์ธ์ด ์ ๋ฌด๋ฅผ ํ๋จํ๊ณ ๊ทธ ๊ฒฐ๊ณผ์ ๋ฐ๋ฅธ ๋ผ์ฐํธ๋ฅผ ๋ ๋๋งํ๊ฒ ํด์คฌ๋ค.
๊ทธ๋ฐ๋ฐ ๋ก๊ทธ์์์ด ๋๋ฉด isLoggedIn์ด false๋ก ๋ฐ๋๊ธฐ ๋๋ฌธ์
false๋ก ๋ถ๊ธฐํ๋ ๋ผ์ฐํธ๊ฐ ๋์ํด์ผํ์ง๋ง, exact path ="/"๋ ์ ํํ ์ฃผ์๊ฐ "/"์ธ ๊ฒฝ์ฐ์๋ง ํด๋น๋๊ธฐ ๋๋ฌธ์, ๋ก๊ทธ์์ ๋ฒํผ์ ์ฌ์ฉํ ํ์ธ ์ฃผ์๊ฐ "/profile"์ด๊ธฐ ๋๋ฌธ์, Route๊ฐ ๋์ํ์ง ์๊ณ
Redirect๊ฐ ๋์ํ์ฌ ๋ฉ์ธํ๋ฉด์ผ๋ก ์ด๋ํ๊ฒ ํด์ค๋ค.
์ฐธ๊ณ ๋งํฌ
https://reactrouter.com/web/api/Redirect
์ฌ์ค์ ์ด๋ฒ ์ฐ์ด๋ถํฐ ๋ธ๋ก๊ทธ ํ๋์ ํ๋ฉด์ ๊ณต๋ถ ๊ธฐ๋ก์ ์์ฑํ๋ ค๊ณ ํ์ง๋ง, ์ฌ๋์ด๋ผ๋๊ฒ ๊ท์ฐฎ์์ ธ์..๋ฏธ๋ฃจ๊ฒ ๋์๋ค..ใ ใ .. ๊ทธ๋ ๊ธฐ๋๋ฌธ์ ๋ฆ์์ง๋ง ์ง๊ธ๋ถํฐ๋ผ๋ ๊พธ์คํ๊ฒ ์์ฑํ๋ ค๊ณ ํ๋ค!