-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (56 loc) · 2.05 KB
/
index.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require("express");
const axios = require("axios");
const qs = require("qs");
const app = express();
const port = 3000;
// 카카오 REST API 키와 Redirect URI 설정 (자신의 값으로 변경)
const REST_API_KEY = "";
const REDIRECT_URI = "http://localhost:3000/login-callback";
// 카카오 로그인 페이지로 리다이렉트하는 라우트
app.get("/login", (req, res) => {
const kakaoAuthURL = `https://kauth.kakao.com/oauth/authorize?client_id=${REST_API_KEY}&redirect_uri=${REDIRECT_URI}&response_type=code`;
res.redirect(kakaoAuthURL);
});
// 카카오 인증 후 Redirect URI로 돌아왔을 때 호출되는 콜백 라우트
app.get("/login-callback", async (req, res) => {
const { code } = req.query; // Authorization Code 받기
try {
// Authorization Code를 이용하여 Access Token 요청
const tokenResponse = await axios({
method: "POST",
url: "https://kauth.kakao.com/oauth/token",
headers: {
"Content-type": "application/x-www-form-urlencoded;charset=utf-8",
},
data: qs.stringify({
grant_type: "authorization_code",
client_id: REST_API_KEY,
redirect_uri: REDIRECT_URI,
code,
}),
});
const { access_token } = tokenResponse.data; // Access Token 추출
// Access Token을 이용하여 사용자 정보 조회
const userResponse = await axios({
method: "GET",
url: "https://kapi.kakao.com/v2/user/me",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-type": "application/x-www-form-urlencoded;charset=utf-8",
},
});
// 사용자 정보를 클라이언트에게 전달
// res.json(userResponse.data);
res.json({
profile_image: userResponse.data.kakao_account.profile.profile_image_url,
profile_nickname: userResponse.data.kakao_account.profile.nickname,
});
} catch (error) {
console.error(error);
res.json(error.response.data);
}
});
// 서버 실행
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});