Oauth.js 3.25 KB
Newer Older
Spark's avatar
Spark committed
1
2
import Swal from 'sweetalert2'
import '../App.css'
Spark's avatar
Spark committed
3
4
5
6
7

// export const KAKAO_AUTH_URL = `https://kauth.kakao.com/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code`;

const { Kakao } = window;

Spark's avatar
Spark committed
8

Spark's avatar
Spark committed
9
10
11
export function LoginWithKakao() {
    //authObj : response.data에 들어가 있는 부분 object 형식
    Kakao.Auth.loginForm({
Spark's avatar
Spark committed
12
13
14
        // 팝업 + 다른 아이디 로그인시
        scope: 'account_email, profile_nickname',
        // 추가 동의 받을 동의 항목 ID 목록, 하나의 문자열에 여러 개의 ID를 쉼표(,)로 구분하여 전달
Spark's avatar
Spark committed
15
16
        success: function (authObj) {
            console.log(JSON.stringify(authObj))
Spark's avatar
Spark committed
17

Spark's avatar
Spark committed
18
            console.log('accT ;;', authObj.access_token)
Spark's avatar
Spark committed
19

Spark's avatar
Spark committed
20
            Kakao.API.request({
Spark's avatar
Spark committed
21
                // 현재 로그인한 사용자의 카카오계정 정보 불러오기
Spark's avatar
Spark committed
22
                url: '/v2/user/me',
Spark's avatar
Spark committed
23
                // 사용자 정보 요청 주소
Spark's avatar
Spark committed
24
                data: {
Spark's avatar
Spark committed
25
26
                    property_keys: ["kakao_account.profile", "kakao_account.email"]
                    // 파라미터를 통해 특정 사용자 정보만 지정해서 요청
Spark's avatar
Spark committed
27
28
29
                },
                success: function (response) {
                    console.log(response);
Spark's avatar
Spark committed
30
                    console.log(response.kakao_account.profile);
Spark's avatar
Spark committed
31

Spark's avatar
Spark committed
32
33
34
35
                    const nickValue = Object.values(response.kakao_account.profile)
                    localStorage.setItem('nickname', nickValue)
                    const nickname = localStorage.getItem('nickname')
                    console.log(nickname)
Spark's avatar
Spark committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
                    Swal.fire({
                        title: '로그인 성공!',
                        text: '🙌 환영합니다 🙌',
                        icon: 'success',
                        customClass: 'swal-wide',
                        confirmButtonText: '확인',
                    }).then((res) => {
                        if (res.isConfirmed) {
                            window.location.replace('/' + '?nickname=' + `${nickname}`)
                        }
                        else {
                            window.location.replace('/' + '?nickname=' + `${nickname}`)
                        }
                    })
Spark's avatar
Spark committed
50
51
52
53
54
                }
            });
        },
        fail: function (err) {
            alert(JSON.stringify(err))
Spark's avatar
Spark committed
55
            console.log(JSON.stringify(err))
Spark's avatar
Spark committed
56
        },
Spark's avatar
Spark committed
57
58
    }
    )
Spark's avatar
Spark committed
59
60
61
}

export function kakaoLogout() {
Spark's avatar
Spark committed
62
63
    // 토큰을 만료시켜 더 이상 해당 액세스 토큰으로 카카오 API를 호출할 수 없도록 
    console.log('geAccesToken()', Kakao.Auth.getAccessToken())
Spark's avatar
Spark committed
64
65
66
    if (!Kakao.Auth.getAccessToken()) {
        alert('Not logged in.')
        localStorage.clear();
Spark's avatar
Spark committed
67
        return;
Spark's avatar
Spark committed
68
69
    }
    Kakao.Auth.logout(function () {
Spark's avatar
Spark committed
70
        // 로그인 시 발급받은 토큰을 만료시키는 함수
Spark's avatar
Spark committed
71
        localStorage.clear();
Spark's avatar
Spark committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        Swal.fire({
            title: '로그아웃 성공!',
            text: '🙏 안녕히 가세요 🙏',
            icon: 'warning',
            customClass: 'swal-wide',
            confirmButtonText: '확인',
        }).then((res) => {
            if (res.isConfirmed) {
                window.location.replace('/')
            }
            else {
                window.location.replace('/')
            }
        })
Spark's avatar
Spark committed
86
    })
Spark's avatar
Spark committed
87
}