From 2446db8ea178c3e6bad0d19bcc24284b977a5ba9 Mon Sep 17 00:00:00 2001 From: hwna00 Date: Wed, 20 Sep 2023 13:01:56 +0900 Subject: [PATCH] =?UTF-8?q?Feat(server):=20=EB=84=A4=EC=9D=B4=EB=B2=84?= =?UTF-8?q?=EB=A1=9C=EB=B6=80=ED=84=B0=20=EC=82=AC=EC=9A=A9=EC=9E=90=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EB=B0=9B=EC=95=84=EC=98=A4=EB=8A=94=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 네이버로부터 받은 code 값과 state 값을 이용하여 access_token을 요청 - access_token을 이용하여 사용자의 정보를 요청 --- packages/server/index.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/server/index.js b/packages/server/index.js index 13a13ba1..0996dad0 100644 --- a/packages/server/index.js +++ b/packages/server/index.js @@ -83,39 +83,42 @@ app.get('/api/users/me', (req, res) => { }); app.get('/naver-callback', async (req, res) => { - code = req.query.code; - state = req.query.state; - const redirectURI = `http://localhost:3000/naver-callback`; + const { code, state } = req.query; + const REDIRECT_URI = `http://localhost:3000/naver-callback`; - api_url = + const API_URI = 'https://nid.naver.com/oauth2.0/token?grant_type=authorization_code&client_id=' + process.env.REACT_APP_NAVER_CLIENT_ID + '&client_secret=' + process.env.REACT_APP_NAVER_CLIENT_SECRET + '&redirect_uri=' + - redirectURI + + REDIRECT_URI + '&code=' + code + '&state=' + state; - const response = await axios.get(api_url, { + const { + data: { access_token }, + } = await axios.get(API_URI, { headers: { 'X-Naver-Client-Id': process.env.REACT_APP_NAVER_CLIENT_ID, 'X-Naver-Client-Secret': process.env.REACT_APP_NAVER_CLIENT_SECRET, }, }); - const { - data: { access_token }, - } = response; - const userProfile = await axios.get('https://openapi.naver.com/v1/nid/me', { + const { + data: { response }, + } = await axios.get('https://openapi.naver.com/v1/nid/me', { headers: { Authorization: 'Bearer ' + access_token, }, }); - console.log(userProfile); + //TODO: response의 사용자 데이터를 DB에 저장하는 로직 추가 필요 + + //TODO: 받지 못한 정보를 받기 위해서 form 페이지로 이동 + res.redirect('http://localhost:8080/thirdparth-callback'); }); app.listen(port, () => {