네이버 SENS api를 이용해 휴대폰 인증문자 보내기
아래 링크에 접속해 이용신청
https://www.ncloud.com/product/applicationService/sens
NAVER CLOUD PLATFORM
cloud computing services for corporations, IaaS, PaaS, SaaS, with Global region and Security Technology Certification
www.ncloud.com
프로젝트 생성
인증키 생성
모듈 생성
// sens.js
function send_message(nickname, phone, authcode) {
// 핸드폰 번호 닉네임, 코드이름
const user_phone_number = phone;
const user_nickname = nickname;
const user_authcode = authcode
// 모듈들을 불러오기. 오류 코드는 맨 마지막에 삽입 예정
const finErrCode = 404;
const axios = require('axios');
const CryptoJS = require('crypto-js');
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;
// 그 외 url 관련
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 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);
axios({
method: method,
// request는 uri였지만 axios는 url이다
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: `${user_nickname}님 푸댕에 가입해주셔서 감사합니다. \n인증번호는 ${user_authcode} 입니다`,
messages: [
// 신청자의 전화번호
{ to: `${user_phone_number}`, },],
},
}).then((res) => {
console.log(res.data);
}).catch((err) => {
console.error(err);
})
return finErrCode
}
module.exports = send_message;
서비스 계층에서 함수 실행
- 전달 받은 인자를 매개변수로 전달하고 함수실행
authCodeSend = async (nickname, phoneNum) => {
try {
const authcode = createAuthCode();
await this.userRepository.authCodeSend(authcode, phoneNum)
send_message(nickname, phoneNum, authcode)
return { "message": "메세지 발송완료" }
} catch (error) {
console.error(error);
return { "errorMessage": "인증번호 요청에 실패하였습니다." }
}
전송된 문자