Oauth.js 3.35 KB
Newer Older
1
import axios from 'axios';
Spark's avatar
Spark committed
2
3
import Swal from 'sweetalert2'
import '../App.css'
Spark's avatar
Spark committed
4
import { routesClient } from './../routesClient';
Spark's avatar
Spark committed
5
6
7
8
9

// 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
10

Spark's avatar
Spark committed
11
12
13
export function LoginWithKakao() {
    //authObj : response.data에 들어가 있는 부분 object 형식
    Kakao.Auth.loginForm({
Spark's avatar
Spark committed
14
15
16
        // 팝업 + 다른 아이디 로그인시
        scope: 'account_email, profile_nickname',
        // 추가 동의 받을 동의 항목 ID 목록, 하나의 문자열에 여러 개의 ID를 쉼표(,)로 구분하여 전달
Spark's avatar
Spark committed
17
18
        success: function (authObj) {
            console.log(JSON.stringify(authObj))
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
                success: async function (response) {
Spark's avatar
Spark committed
29
                    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
                    const nickValue = response.kakao_account.profile['nickname']
                    const emailValue = response.kakao_account.email
34

Spark's avatar
Spark committed
35
                    await axios.post(routesClient.signup, { email: emailValue, nick_name: nickValue, isOauth: true })
36
37
                        .then((res) => console.log('kakao', res))

Spark's avatar
Spark committed
38
                    // localStorage.setItem('login', true)
Spark's avatar
Spark committed
39
40
41
42
43
44
45
46
                    Swal.fire({
                        title: '로그인 성공!',
                        text: '🙌 환영합니다 🙌',
                        icon: 'success',
                        customClass: 'swal-wide',
                        confirmButtonText: '확인',
                    }).then((res) => {
                        if (res.isConfirmed) {
47
                            // window.location.replace('/')
Spark's avatar
Spark committed
48
49
                        }
                        else {
50
                            // window.location.replace('/')
Spark's avatar
Spark committed
51
52
                        }
                    })
Spark's avatar
Spark committed
53
54
55
56
57
                }
            });
        },
        fail: function (err) {
            alert(JSON.stringify(err))
Spark's avatar
Spark committed
58
            console.log(JSON.stringify(err))
Spark's avatar
Spark committed
59
        },
Spark's avatar
Spark committed
60
61
    }
    )
Spark's avatar
Spark committed
62
63
64
}

export function kakaoLogout() {
Spark's avatar
Spark committed
65
66
    // 토큰을 만료시켜 더 이상 해당 액세스 토큰으로 카카오 API를 호출할 수 없도록 
    console.log('geAccesToken()', Kakao.Auth.getAccessToken())
Spark's avatar
Spark committed
67
68
69
    if (!Kakao.Auth.getAccessToken()) {
        alert('Not logged in.')
        localStorage.clear();
Spark's avatar
Spark committed
70
        return;
Spark's avatar
Spark committed
71
72
    }
    Kakao.Auth.logout(function () {
Spark's avatar
Spark committed
73
        // 로그인 시 발급받은 토큰을 만료시키는 함수
Spark's avatar
Spark committed
74
        localStorage.clear();
Spark's avatar
Spark committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        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
89
    })
Spark's avatar
Spark committed
90
}