๐ŸŸฉ Frontend-HTML & CSS & +JS/๐Ÿ”ฐJavaScript

[JS] ์นด์นด์˜คํ†ก ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ/ํƒˆํ‡ดํ•˜๊ธฐ ๋ฐฉ๋ฒ• ๋ฐ html ํŒŒ์ผ (์ฝ”๋“œ์žˆ์Œ)

ramen2 2025. 1. 24. 20:36
๋ฐ˜์‘ํ˜•

1.์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ์—์„œ web์šฉ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ฌ

์‚ฌ์ดํŠธ ๋„๋ฉ”์ธ์€ ๋กœ์ปฌ์ด๋ผ ๋กœ์ปฌ๋กœ ์ง€์ •

 

2. ๋“ฑ๋กํ•˜๋Ÿฌ๊ฐ€๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ, ๋กœ๊ทธ์ธ ์„ฑ๊ณตํ›„ ๊ฐˆ ๊ณณ ์ง€์ •

 

 

 

3. ์•„๋ž˜์˜ ๋™์˜ํ•ญ๋ชฉ ๋„ฃ์Œ

(๋”ํ•˜๋ ค๋ฉด ๋น„์ฆˆ๋‹ˆ์Šค ๊ณ„์ • ๋ณ€ํ™˜ ํ•„์š”)

 

 

 

 

4. js ์•ฑํ‚ค ๋ณต์‚ฌ ํ•ด๋†“๊ณ  html ๋‘˜ ๋‹ค์— ๋„ฃ์Œ

 

 

 

 

 

 

 

 

 

| ํด๋”๊ตฌ์„ฑ

์ด๋ฏธ์ง€๋Š” ๋Œ€๋žต '์นดํ†ก๋กœ๊ธด๋ฒ„ํŠผ'

 

 

| index.html (๋กœ๊ทธ์ธ ํ•  ๊ณณ ์ฝ”๋“œ)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>๋งค์ธ ํŽ˜์ด์ง€</title>
</head>
<body>
    <a href="#0" id="kakaoLogin"><img src="./kakao_login.png" alt="์นด์นด์˜ค๊ณ„์ • ๋กœ๊ทธ์ธ" style="height: 100px;" /></a>
    <p>ํ•ด๋‹น Live Demo๋Š” ํ…Œ์ŠคํŠธ์šฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ์†Œ์Šค ์ฝ”๋“œ์— '๋ณธ์ธ API ํ‚ค'๋ฅผ ๋„ฃ์–ด์•ผ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.</p>
    <script src="https://developers.kakao.com/sdk/js/kakao.js"></script>
    <script>
        // Kakao SDK ์ดˆ๊ธฐํ™”
        window.Kakao.init('bdddddddddddd4');
        // ๋ณธ์ธ js api ํ‚ค ๋„ฃ์–ด์•ผํ•จ

        // ๋กœ๊ทธ์ธ ํ•จ์ˆ˜
        function kakaoLogin() {
            window.Kakao.Auth.login({
                scope: 'profile_nickname, profile_image', // ๋‹‰๋„ค์ž„๊ณผ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ (๋‹ค๋ฅธ๊ฑฐ ์“ฐ๋ ค๋ฉด ์นดํ†ก๊ฐœ๋ฐœ์ž์— ๋น„์ฆˆ๋‹ˆ์Šค ์‹ ์ฒญํ•ด์„œ email๋“ฑ๋„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜์žˆ์Œ)
                success: function(response) {
                    const token = response.access_token;

                    // ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ
                    window.Kakao.API.request({
                        url: '/v2/user/me',
                        success: (res) => {
                            const kakao_account = res.kakao_account;
                            const userInfo = {
                                nickname: kakao_account.profile.nickname,
                                profile_image: kakao_account.profile.profile_image_url,
                            };

                            // ์‚ฌ์šฉ์ž ์ •๋ณด์™€ ํ† ํฐ์„ sessionStorage์— ์ €์žฅ
                            sessionStorage.setItem('AccessKEY', token);
                            sessionStorage.setItem('UserInfo', JSON.stringify(userInfo));

                            // ๋กœ๊ทธ์ธ ์„ฑ๊ณต ํ›„ ๋ฆฌ๋‹ค์ด๋ ‰์…˜
                            window.location.href = '/ex/kakao_login.html';
                        },
                        fail: (error) => {
                            console.error('์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ์‹คํŒจ', error);
                        }
                    });
                },
                fail: function(error) {
                    console.error('๋กœ๊ทธ์ธ ์‹คํŒจ', error);
                }
            });
        }

        // ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
        const login = document.querySelector('#kakaoLogin');
        login.addEventListener('click', kakaoLogin);
    </script>
</body>
</html>

 

๋กœ๊ทธ์ธ

 

| kakao_login.html (๋กœ๊ทธ์ธ ํ•œ ํ›„ redirection ๋  ๊ณณ ์ฝ”๋“œ)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redirect URI</title>
</head>
<style>
    #profile {
        width: 400px;
        height: 400px;
        background-color: antiquewhite;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 20px;
        text-align: center;
    }
    #profile img {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        object-fit: cover;
    }
</style>
<body>
    <div id="profile">
        <!-- ํ”„๋กœํ•„ ์ •๋ณด ํ‘œ์‹œ -->
        <p>๋กœ๋”ฉ ์ค‘...</p>
    </div>
    <a href="#0" id="kakaoLogout">๋กœ๊ทธ์•„์›ƒ</a> | 
    <a href="#0" id="secession">ํƒˆํ‡ด</a>

    <script src="https://developers.kakao.com/sdk/js/kakao.js"></script>
    <script>
        // Kakao SDK ์ดˆ๊ธฐํ™”
        window.Kakao.init('bdddddddddddd4');
        // ๋ณธ์ธ js api ํ‚ค ๋„ฃ์–ด์•ผํ•จ

        // sessionStorage์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
        const userInfo = JSON.parse(sessionStorage.getItem('UserInfo'));
        const profileDiv = document.querySelector('#profile');

        // ์‚ฌ์šฉ์ž ์ •๋ณด ํ‘œ์‹œ
        if (userInfo) {
            profileDiv.innerHTML = `
                <img src="${userInfo.profile_image}" alt="ํ”„๋กœํ•„ ์ด๋ฏธ์ง€" />
                <p>๋‹‰๋„ค์ž„: ${userInfo.nickname}</p>
            `;
        } else {
            profileDiv.innerHTML = `<p>์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.</p>`;
        }

        // ๋กœ๊ทธ์•„์›ƒ ๊ธฐ๋Šฅ
        function kakaoLogout() {
            if (!Kakao.Auth.getAccessToken()) {
                console.log('Not logged in.');
                return;
            }
            Kakao.Auth.logout(function() {
                alert('๋กœ๊ทธ์•„์›ƒ ์™„๋ฃŒ');
                sessionStorage.clear(); // ๋ชจ๋“  ์„ธ์…˜ ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ
                window.location.href = '/';
            });
        }

        // ํƒˆํ‡ด ๊ธฐ๋Šฅ
        function secession() {
            Kakao.API.request({
                url: '/v1/user/unlink',
                success: function() {
                    alert('ํƒˆํ‡ด ์™„๋ฃŒ');
                    sessionStorage.clear();
                    window.location.href = '/';
                },
                fail: function(error) {
                    console.error('ํƒˆํ‡ด ์‹คํŒจ', error);
                },
            });
        }

        // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์—ฐ๊ฒฐ
        document.querySelector('#kakaoLogout').addEventListener('click', kakaoLogout);
        document.querySelector('#secession').addEventListener('click', secession);
    </script>
</body>
</html>

๋กœ๊ทธ์ธ ํ›„

 

 

 

| ํŒŒ์ผ ๋‹ค์šด

kakaoLogin-๊ณต์œ ์šฉ-apiํ‚ค๋ณธ์ธ๊ฒƒ+์›น์ฃผ์†Œ๋“ฑ๋กํ•˜์—ฌ์‚ฌ์šฉ.zip
0.02MB

 

 

๋ฐ˜์‘ํ˜•