Skip to content

Commit

Permalink
Feat(server): 네이버로부터 사용자 정보 받아오는 로직 추가
Browse files Browse the repository at this point in the history
- 네이버로부터 받은 code 값과 state 값을 이용하여 access_token을 요청
- access_token을 이용하여 사용자의 정보를 요청
  • Loading branch information
hwna00 committed Sep 25, 2023
1 parent 43ef682 commit 2446db8
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down

0 comments on commit 2446db8

Please sign in to comment.