Oauth.js 3.06 KB
Newer Older
Spark's avatar
Spark committed
1
2
3
4
5
6
7
8
9
10
11
12
import axios from 'axios';
import { Spinner, Button, Col, Row } from 'react-bootstrap';
import { useHistory } from 'react-router-dom';


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

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
18
        success: function (authObj) {
            alert('로그인 되었습니다. @@@@@@@@@')
            console.log(JSON.stringify(authObj))
Spark's avatar
Spark committed
19

Spark's avatar
Spark committed
20
21
22
            console.log('accT ;;', authObj.access_token)
            console.log(typeof (localStorage))
            console.log(Object.keys(localStorage)[0])
Spark's avatar
Spark committed
23

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

Spark's avatar
Spark committed
36
37
38
39
40
                    const nickValue = Object.values(response.kakao_account.profile)
                    localStorage.setItem('nickname', nickValue)
                    const nickname = localStorage.getItem('nickname')
                    console.log(nickname)
                    window.location.replace('/' + '?nickname=' + `${nickname}`)
Spark's avatar
Spark committed
41
42
43
44
45
                }
            });
        },
        fail: function (err) {
            alert(JSON.stringify(err))
Spark's avatar
Spark committed
46
        },
Spark's avatar
Spark committed
47
48
49
50
    })
}

export function kakaoLogout() {
Spark's avatar
Spark committed
51
52
    // 토큰을 만료시켜 더 이상 해당 액세스 토큰으로 카카오 API를 호출할 수 없도록 
    console.log('geAccesToken()', Kakao.Auth.getAccessToken())
Spark's avatar
Spark committed
53
54
55
    if (!Kakao.Auth.getAccessToken()) {
        alert('Not logged in.')
        localStorage.clear();
Spark's avatar
Spark committed
56
        return;
Spark's avatar
Spark committed
57
58
    }
    Kakao.Auth.logout(function () {
Spark's avatar
Spark committed
59
        // 로그인 시 발급받은 토큰을 만료시키는 함수
Spark's avatar
Spark committed
60
61
62
63
64
65
66
        alert('logout ok\naccess token -> ' + Kakao.Auth.getAccessToken())
        localStorage.clear();
        window.location.replace('/')
    })
}


Spark's avatar
Spark committed
67
export function Loading() {
Spark's avatar
Spark committed
68
69
70

    return (
        <Row className='d-block'>
Spark's avatar
Spark committed
71

Spark's avatar
Spark committed
72
73
74
75
76
77
78
79
80
            <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
81
}