Created
November 26, 2018 14:57
-
-
Save mooyoul/a572242821cd507292fcfab1172b37dc to your computer and use it in GitHub Desktop.
kakao
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const { send, sendError } = require('micro'); | |
const { URL, URLSearchParams } = require('url'); | |
const KAKAO_APP_KEY = 'YOUR_KAKAO_APP_KEY'; | |
const KAKAO_REDIRECT_URL = 'http://www.lvh.me:3000/authorize/callback'; | |
const KAKAO_AUTHORIZE_ENDPOINT = (() => { | |
const url = new URL('https://kauth.kakao.com/oauth/authorize'); | |
url.searchParams.append('client_id', KAKAO_APP_KEY); | |
url.searchParams.append('redirect_uri', KAKAO_REDIRECT_URL); | |
url.searchParams.append('response_type', 'code'); | |
return url.toString(); | |
})(); | |
module.exports = exports = async (req, res) => { | |
const pathname = parsePathname(req.url); | |
if (pathname === '/authorize') { | |
return redirect(res, KAKAO_AUTHORIZE_ENDPOINT); | |
} | |
if (pathname === '/authorize/callback') { | |
const query = parseQuery(req.url); | |
const code = query.get('code'); | |
if (!code) { | |
return redirect(res, KAKAO_AUTHORIZE_ENDPOINT); | |
} | |
try { | |
const params = new URLSearchParams(new Map([ | |
['grant_type', 'authorization_code'], | |
['client_id', KAKAO_APP_KEY], | |
['redirect_url', KAKAO_REDIRECT_URL], | |
['code', code], | |
])); | |
const res = await axios({ | |
method: 'POST', | |
url: 'https://kauth.kakao.com/oauth/token', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
data: params.toString(), | |
}); | |
return res.data; | |
} catch (e) { | |
if (e.response) { | |
console.error('Request failed with %d', e.response.status, e.response.data); | |
} | |
return sendError(req, res, e); | |
} | |
} | |
return redirect(res, '/authorize'); | |
} | |
function parsePathname(url) { | |
const index = url.indexOf('?'); | |
return index === -1 ? url : url.slice(0, index); | |
} | |
function parseQuery(url) { | |
const index = url.indexOf('?'); | |
if (index === -1) { | |
return new URLSearchParams(); | |
} | |
return new URLSearchParams(url.slice(index + 1)); | |
} | |
function redirect(res, location, status = 302) { | |
res.setHeader('Location', location); | |
return send(res, status); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "kakao-oauth", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"axios": "^0.18.0", | |
"micro": "^9.3.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment