우리는 회원가입 시 이메일, 닉네임, 비밀번호를 받아왔는데 mvp에 이메일 찾기 / 비밀번호 찾기도 있었다. 그러다보니 이메일찾기를 할려면 이메일을 제외한 유니크값이 필요하기에 휴대폰 번호를 넣었다. 또한 휴대폰 번호가 유니크 값이기에 다른 사람의 휴대폰번호로 회원가입을 막아야하기에 휴대폰 인증까지 넣었다. sms를 보내는 방법은 여러가지이지만 우리는 Naver cloud platform Simple & Easy Notification Service를 도입했다.
구글링해보면 도입하는 방법은 잘 나와있기에 생략하고 굳이 우리 서버에 인증을 둘 필요가 없기에 이메일 인증과 마찬가지로 람다로 뺐다.
exports.lambdaHandler = async (event, context) => {
const axios = require("axios");
const CryptoJS = require("crypto-js");
const user_phone_number = event.phoneNumber;
const date = Date.now().toString();
const serviceId = process.env.SENS_SERVICE_ID;
const secretKey = process.env.SENS_SECRET_KEY;
const accessKey = process.env.SENS_ACCESS_KEY;
const my_number = process.env.SENS_MYNUM;
const method = "POST";
const space = " ";
const newLine = "\n";
const url = `https://sens.apigw.ntruss.com/sms/v2/services/${serviceId}/messages`;
const url2 = `/sms/v2/services/${serviceId}/messages`;
const generateRandom = function (min, max) {
const ranNum = Math.floor(Math.random() * (max - min + 1)) + min;
return ranNum;
};
const number = generateRandom(111111, 999999);
const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secretKey);
hmac.update(method);
hmac.update(space);
hmac.update(url2);
hmac.update(newLine);
hmac.update(date);
hmac.update(newLine);
hmac.update(accessKey);
const hash = hmac.finalize();
const signature = hash.toString(CryptoJS.enc.Base64);
await axios({
method: method,
url: url,
headers: {
"Contenc-type": "application/json; charset=utf-8",
"x-ncp-iam-access-key": accessKey,
"x-ncp-apigw-timestamp": date,
"x-ncp-apigw-signature-v2": signature,
},
data: {
type: "SMS",
countryCode: "82",
from: my_number,
content: `[PillNuts] 인증번호 [${number}]를 입력해주세요.`,
messages: [{ to: `${user_phone_number}` }],
},
})
return {
message: `인증문자 전송에 성공하였습니다.`,
code: number,
};
};
코드는 이런 식으로 되어있고 crypto-js를 이용해서 암호화를 해서 넣는 식 같았다. 인증메일은 잘 작동하였고 이메일 인증과 같이 앞에 api-gateway를 붙였다. 유료서비스이기에 api-key가 있어야만 요청을 보낼 수 있게 하였다.
'TIL' 카테고리의 다른 글
| AWS API Gateway lambda 권한부여자 (1) | 2023.01.30 |
|---|---|
| AWS API Gateway 권한 (0) | 2023.01.29 |
| PresignedURL S3 - Lambda image Resizing (0) | 2023.01.15 |
| nodemailer + AWS lambda (0) | 2023.01.04 |
| github actions deploy to ecs (0) | 2022.12.25 |