Oauth.js 2.88 KB
Newer Older
Spark's avatar
Spark committed
1
import { Spinner, Button, Row } from 'react-bootstrap';
Spark's avatar
Spark committed
2
3
4
5
6
7
8
9
10


// 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
11
12
13
        // 팝업 + 다른 아이디 로그인시
        scope: 'account_email, profile_nickname',
        // 추가 동의 받을 동의 항목 ID 목록, 하나의 문자열에 여러 개의 ID를 쉼표(,)로 구분하여 전달
Spark's avatar
Spark committed
14
15
16
        success: function (authObj) {
            alert('로그인 되었습니다. @@@@@@@@@')
            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
36
                    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
37
38
39
40
41
                }
            });
        },
        fail: function (err) {
            alert(JSON.stringify(err))
Spark's avatar
Spark committed
42
        },
Spark's avatar
Spark committed
43
44
45
46
    })
}

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


Spark's avatar
Spark committed
63
export function Loading() {
Spark's avatar
Spark committed
64
65
66
67
68
69
70
71
72
73
74
    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
75
}