Skip to content

Commit

Permalink
feat: Add social login code
Browse files Browse the repository at this point in the history
  • Loading branch information
dokdo2013 committed Nov 18, 2024
1 parent 8e43d4c commit e9ed771
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 4 deletions.
2 changes: 1 addition & 1 deletion chapter1/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ app.get("/todos", (req, res) => {
// Todo 추가
app.post("/todos", (req, res) => {
const { title } = req.body;
const newTodo = { id: todoIdCursor, title };
const newTodo = { id: todoIdCursor++, title };
todos.push(newTodo);
res.status(201).json(newTodo);
});
Expand Down
66 changes: 66 additions & 0 deletions chapter2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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}`);
});
107 changes: 104 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.7.7",
"express": "^4.21.1",
"qs": "^6.13.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1"
}
Expand Down

0 comments on commit e9ed771

Please sign in to comment.