Oauth.js 3.81 KB
Newer Older
Spark's avatar
Spark committed
1
2
3
import { Spinner, Button, Row, Alert } from 'react-bootstrap';
import Swal from 'sweetalert2'
import '../App.css'
Spark's avatar
Spark committed
4
5
6
7
8

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

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

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

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

Spark's avatar
Spark committed
33
34
35
36
                    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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
                    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
51
52
53
54
55
                }
            });
        },
        fail: function (err) {
            alert(JSON.stringify(err))
Spark's avatar
Spark committed
56
            console.log(JSON.stringify(err))
Spark's avatar
Spark committed
57
        },
Spark's avatar
Spark committed
58
59
    }
    )
Spark's avatar
Spark committed
60
61
62
}

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


Spark's avatar
Spark committed
91
export function Loading() {
Spark's avatar
Spark committed
92
93
94
95
96
97
98
99
100
101
102
    return (
        <Row className='d-block'>
            <Button id='formbtn' className='d-flex justify-content-center align-items-center m-auto' style={{ width: '200px', height: '200px', flexDirection: 'column' }} disabled>
                <Spinner animation="border" role="status">
                    <span className="sr-only">Loading...</span>
                </Spinner>
                <br />
                Loading...
            </Button>
        </Row>
    )
Spark's avatar
Spark committed
103
}