[Next] SNS๋ณ๋ก ๋์ ๋ผ์ฐํ (Dynamic Routes) ์ง์ ํ๊ธฐ
๊ฐ์ ํ๋ฉด๊ตฌ์ฑ์ ๊ฐ์ API์ธ ์์ ์ด์ด์
ํ์ด์ง๋ฅผ ๋ฐ๋ก ๋ง๋ค ํ์๊ฐ ์์ด์ ๋ชจ๋ SNS ๋ก๊ทธ์ธ์ Oauth.tsxํ์ผ๋ก ํ๊ธฐ๋ก ํ๋ค.
๊ทธ๋ฌ๋ค๋ณด๋ sns๋ณ๋ก ์ฝ๋๋ฅผ ์๋ฒ๋จ์ผ๋ก ๋ณด๋ด์ค์ผ๋๋๋ฐ,
์ด๋ ๋ผ์ฐํธ๋ฅผ ์ด์ฉํด์ ๋ด์์ ๋ณด๋ด์ค
๊ทผ๋ฐ Redirect URI๋ฅผ ๋ณ๊ฒฝํด์ผํ๋ ์กฐ๊ธ ๋ฌ๋ผ์ ๊ฒธ์ฌ๊ฒธ์ฌ ๊ธฐ๋กํด๋๋๋ค
[] ๋๊ดํธ๋ฅผ ์ฌ์ฉํด์ ํด๋๋ฅผ ๋ง๋ค๊ณ ๊ทธ ์์ oauth.ts ํ์ผ์ ๋ง๋ฆ (next13์ดํ ๋ถํฐ๋ oauthํด๋์ page.tsxํ์ผ๋ก)
oauth.tsํ์ผ์ ์ปดํฌ๋ํธ ํ์ด์ง๋ก ๋ฐ๋ก ์ด๋๋๋ค.
๊ธฐ์กด์ NEXT_PUBLIC_OAUTH_REDIRECT๋ http://localhost:3000/login/oauth์ธ๋ฐ,
๋ค์oauth๋ฅผ ๋นผ์ฃผ๊ณ redirect URI๋ฅผ ์ฌ์ค์ ํด์คฌ๋ค
WebLogin.tsx
const loginWithKakao = (snsType: string | null) => {
Kakao.Auth.authorize({
redirectUri: `${process.env.NEXT_PUBLIC_OAUTH_REDIRECT}/${snsType}/oauth`,
scope: "profile_nickname",
});
};
...
<button onClick={() => loginWithKakao("kakao")} type="button"> ....
(๋น์ฐํ kakao developers์๋ ์ฃผ์๋ฅผ ๋ฐ๊พธ์ด์ฃผ์ด์ผํ๊ณ , ์๋ฒ๋จ์๋ ๋งํด์ค์ผํจ)
์ด๋ ๊ฒ ํด์ฃผ๊ณ ์นด์นด์ค ๋ก๊ทธ์ธ ๋ฒํผ์ ๋๋ฌ๋ณด์๋๋ ์์ฃผ ์ ๋ถ๋ฌ์ค๋ ๋ชจ์ต์ ๋ณผ ์ ์์๋ค!
Oauth.tsx
const snsIdentifierCodes = {
kakao: "~~501",
naver: "~~502",
google: "~~503",
};
์ฝ๋๋ฅผ ๋ณ์๋ก ์ง์ ํด์ฃผ๊ณ , (ํ์ ์๋ต)
const { code: authCode, sns: snsType } = router.query;
router.query.๋ฅผ ์ด์ฉํด์ ๋ถ๋ฌ์ฌ ์ ์๋ค.
๊ทธ๋์ snsCode: snsIdentifierCodes[snsType] ์ด๋ฐ์์ผ๋ก ์ฐ๋ฉด ๋จ!