๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

node.js

[Node.js] ์• ํ”Œ๋กœ๊ทธ์ธ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ”‘ Apple Developer์—์„œ key ๋ฐœ๊ธ‰๋ฐ›๊ธฐ

Apple Developer ์‚ฌ์ดํŠธ์—์„œ ๋กœ๊ทธ์ธํ•˜๊ณ , Account์— ๋“ค์–ด๊ฐ€์„œ Identifier๊ณผ App ID๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  Authkey-.p8ํŒŒ์ผ๊ณผ id๊ฐ’๋“ค์ด ์ค€๋น„ ๋œ ํ›„, ๊ตฌํ˜„ ๋ถ€๋ถ„์„ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค!

๐ŸŒฑ Apple-auth ๊ธฐ๋ณธ ์„ธํŒ…

Apple-auth๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์• ํ”Œ ๋กœ๊ทธ์ธ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
npm install apple-auth๋กœ ์„ค์น˜ ๋จผ์ € ํ•ด์ค๋‹ˆ๋‹ค.
์ดํ›„ configํŒŒ์ผ์„ ์ค€๋น„ํ•ด์•ผ ํ•˜๋Š”๋ฐ config.json ํŒŒ์ผ์˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. Apple Developer ์‚ฌ์ดํŠธ์—์„œ ์ƒ์„ฑํ•œ ๊ฐ’๋“ค๋กœ ์•„๋ž˜๋ฅผ ์ฑ„์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•˜๊ฒŒ ์˜ˆ์‹œ๋ฅผ ์ ์–ด๋‘์—ˆ์œผ๋‹ˆ ๊ผญ ์ž˜๋งž์ถฐ์„œ ์ ์–ด์ฃผ์„ธ์š”~! (์ €๋Š” -.p8 ํ‚ค ํŒŒ์ผ์˜ ์ด๋ฆ„๋„ private_key_path๋กœ ๋นผ์คฌ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ ๋นผ์ง€ ์•Š๊ณ  ๋ผ์šฐํŒ…์—์„œ ๊ทธ๋ƒฅ ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผํ•ด๋„ ๋ฌด๊ด€ํ•ฉ๋‹ˆ๋‹ค.)

{ "client_id": "com.****", "team_id": "AB1234CDE3", "key_id": "AB12CD123E", "redirect_uri": "https://apple-auth.example.com/auth", "private_key_path": "AuthKey_AB12CD123E.p8" }

{
    "client_id": "com.****",
    "team_id": "AB1234CDE3",
    "key_id": "AB12CD123E",
    "redirect_uri": "https://apple-auth.example.com/auth",
    "private_key_path": "AuthKey_AB12CD123E.p8"
}

๐ŸŒณ ๋กœ๊ทธ์ธ ๊ตฌํ˜„ํ•˜๊ธฐ

ํ•„์š”ํ•œ ํŒŒ์ผ๊ณผ ๋ชจ๋“ˆ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

const jwt = require('jsonwebtoken');
const path = require('path');
const AppleAuth = require('apple-auth');
const appleConfig = require('../../../../config/apple/apple.json');
const auth = new AppleAuth(appleConfig, path.join(__dirname, `../../../../config/apple/${appleConfig.private_key_path}`));

ํด๋ผ์ด์–ธํŠธ์—์„œ code๊ฐ’์„ ๋ฐ›์•„์™€์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํ•ด๋…ํ•˜๋ฉด ํ•„์š”ํ•œ ๊ฐ’๋“ค์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต sub๊ฐ’์„ ๋””๋น„์— ์ €์žฅํ•ด๋‘๊ณ  ์ด๋ฅผ ํ†ตํ•ด์„œ ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

let { code } = req.body;
if (!code) { 
  res.status(200).json(NULL_VALUE);
  return;
} 
const response  = await auth.accessToken(code);
const idToken = jwt.decode(respon.id_token);
const email = idToken.email;
const sub = idToken.sub;

์œ„ ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ๋””๋น„์— sub๊ฐ’๊ณผ email์„ ์ €์žฅํ•ด๋‘๊ณ  ๋””๋น„์— ์ด๋ฏธ ์ €์žฅ๋œ ๊ฐ™์€ ์‚ฌ์šฉ์ž๊ฐ€ ์—†์œผ๋ฉด ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๋„๋ก ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. apple-auth๊ฐ€ ์›Œ๋‚™ ์ž˜ ๋งŒ๋“ค์–ด์ ธ ์žˆ์–ด์„œ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์—ˆ์—ˆ์Šต๋‹ˆ๋‹ค!

 

๐Ÿ‚ ์ฐธ๊ณ 

apple-auth example github

โ›” apple-signin

์ €๋Š” apple-signin ๋ชจ๋“ˆ์„ ์ผ์„ ๋•Œ, ๋ฐ˜๋ณต์ ์œผ๋กœ ๋กœ๊ทธ์ธ์„ ์‹œ๋„ํ•˜๋ฉด ํ† ํฐ์„ ํ•ด๋…ํ•˜์ง€ ๋ชปํ•˜๋Š” invalid token (์˜ค๋ฅ˜๋ฉ”์„ธ์ง€๋Š” ์ •ํ™•ํžˆ ๊ธฐ์–ต๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค..!)์™€ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ ๋•Œ๋ฌธ์— app store ๋ฐฐํฌ์—์„œ ๋ฆฌ์ ๋์—ˆ์Šต๋‹ˆ๋‹ค. ์•Œ์•„๋ณด๋‹ˆ apple-signin์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์ €๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๊ฒช์€ ๋ถ„๋“ค์ด ๊ณ„์‹œ๋”๋ผ๊ตฌ์š”! apple-auth๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค~

 

๋ฐ˜์‘ํ˜•