Oauth.js 5.54 KB
Newer Older
Spark's avatar
Spark committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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({
        scope: 'account_email',
        success: function (authObj) {
            alert('로그인 되었습니다. @@@@@@@@@')
            console.log(JSON.stringify(authObj))
            const accToken = authObj.access_token
            localStorage.setItem('Kakao_token', accToken)
            console.log('accT ;;', authObj.access_token)
            console.log(typeof (localStorage))
            console.log(Object.keys(localStorage)[0])
            Kakao.API.request({
                url: '/v2/user/me',
                data: {
                    property_keys: ["kakao_account.email"]
                },
                success: function (response) {
                    console.log(response);
                    console.log(response.kakao_account.email);

                    const userEmail = response.kakao_account.email.split('@')[0];
Spark's avatar
Spark committed
33
34
35
36
                    localStorage.setItem('user_email', userEmail)
                    const user_email = localStorage.getItem('user_email')
                    console.log(user_email)
                    // window.location.replace('/' + '?kakaoemail='+ `${user_email}`)
Spark's avatar
Spark committed
37
                }
Spark's avatar
Spark committed
38

Spark's avatar
Spark committed
39
            });
Spark's avatar
Spark committed
40

Spark's avatar
Spark committed
41
42
43
44
45
46


        },
        fail: function (err) {
            alert(JSON.stringify(err))

Spark's avatar
Spark committed
47
        },
Spark's avatar
Spark committed
48
49
    })

Spark's avatar
Spark committed
50

Spark's avatar
Spark committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
}

export function kakaoLogout() {
    if (!Kakao.Auth.getAccessToken()) {
        alert('Not logged in.')
        localStorage.clear();
        window.location.replace('/')
        return
    }
    Kakao.Auth.logout(function () {
        alert('logout ok\naccess token -> ' + Kakao.Auth.getAccessToken())
        localStorage.clear();
        window.location.replace('/')

    })

}

export function unlinkApp() {
    Kakao.API.request({
        url: '/v1/user/unlink',
        success: function (res) {
            alert('success: ' + JSON.stringify(res))
            window.location.replace('/')

        },
        fail: function (err) {
            alert('fail: ' + JSON.stringify(err))
            window.location.replace('/')

        },

    })

}

function Oauth() {
    const history = useHistory()
    const REST_API_KEY = 'a564b730d6339b084ecf7775a9a09284';
    const REDIRECT_URI = 'http://localhost:3000/oauth';
    const LOGOUT_REDIRECT_URI = 'http://localhost:3000/logout'

    const url = new URL(window.location.href)
    const authorizationCode = url.searchParams.get('code')
    console.log('인가 코드', authorizationCode);


    const kakaoAuthURl = `https://kauth.kakao.com/oauth/token`
    // const kakaoAuthURl = `https://kauth.kakao.com/oauth/authorize?client_id=${REST_API_KEY}&redirect_uri=${REDIRECT_URI}&response_type=code`

    // const payload = {
    //     grant_type: "authorization_code",
    //     client_id: `${REST_API_KEY}`,
    //     redirect_uri: `${REDIRECT_URI}`,
    //     code: `${authorizationCode}`
    // }


    const params = new URLSearchParams();
    // PAYLOAD
    params.append('grant_type', 'authorization_code')
    params.append('client_id', `${REST_API_KEY}`)
    params.append('redirect_uri', `${REDIRECT_URI}`)
    params.append('code', `${authorizationCode}`)

    axios.post(kakaoAuthURl, params).then(response => {
        console.log('RES_DATA ;;;', response.data);
        console.log('acc_token ;;', response.data.access_token);
        const accToken = response.data.access_token;
        if (accToken) {
            console.log('YES login!!!!!!!!!!!!!');
            localStorage.setItem('Kakao_token', accToken)
            history.push('/')
        }
    })






    // const res = axios.post(kakaoAuthURl, params)
    // console.log(res)
    // const accessToken = res.data
    // console.log(accessToken)

    // setTimeout(window.location.replace('/'), 3000)


    // const ischecked = () => {
    //     if (checkAuth) {
    //         return true;
    //     }
    //     else {
    //         return false;
    //     }

    // }
    // console.log(ischecked())

    // const isOauthChecked = () => {
    //     if (checkAuth.access_token) {
    //         console.log(true)
    //         return true;
    //     }
    // }

    //     {
    //     console.log('accccc', response.data.access_token);
    //     console.log('%%%', response.data);
    //     if (response.data.access_token) {
    //         console.log('YES login!!!!!!!!!!!!!');
    //         isOauth(true)
    //         console.log('@@@12', isOauth(true))
    //         setTimeout(window.location.replace('/'), 3000)
    //     }
    //     else {
    //         isOauth(false)
    //     }
    // })

    return (
        <Row className='d-block'>
            <Col className='text-center m-auto' style={{ fontSize: '5px' }} >
                {/* 인가코드 : {authorizationCode}
                <br />
                Params : {params} */}
            </Col>
            <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>
    )
}

export default Oauth;