// : Express 사용
const express = require('express');
const router = express.Router();
// : 암호화
const CrpytoJS = require('crypto-js');
const encodeKey = 'saint place'; // :: 암호화 키
// : 유니크 아이디
const {v4: uuid4} = require('uuid');
// : 데이터베이스 커넥트
const connectPool = require('../connectPool');
router.post('/', async (require, response) => {
// :: 받아옴
const { userName, userEmail, userPassword } = require.body;
// :: 유니크 아이디 생성
const userID = uuid4();
// :: 패스워드 암호화
const password = CrpytoJS.AES.encrypt(userPassword, encodeKey).toString();
// :: 암호화 확인
const encodePass = password;
console.log("encode Password : " + encodePass);
// :: 복호화 확인
const decodePass = CrpytoJS.AES.decrypt(encodePass, encodeKey).toString(CrpytoJS.enc.Utf8);
console.log("decode Password : " + decodePass);
// :: 쿼리 생성
const query = "insert into users(userID, userName, userEmail, userPassword) values (?, ?, ?, ?);";
// :: 쿼리 적용
try {
let[rows, fields] = await connectPool.promise()
.query(query, [userID, userName, userEmail, password]); // :: 적용
response.sendStatus(200);
} catch(error) {
response.sendStatus(500);
}
});
router.delete('/', async (require, response) => {
console.log('회원목록 가져오기 테스트');
try {
const query = "select * from users";
let[rows, fields] = await connectPool.promise().query(query);
rows.forEach(row => {
const name = row.userName;
const email = row.userEmail;
const password = CrpytoJS.AES.decrypt(row.userPassword, encodeKey).toString(CrpytoJS.enc.Utf8);
const time = row.userConnectedTime;
console.log(`이름 : ${name}, 이메일 : ${email}, 패스워드 : ${password}, 시간 : ${time}`);
});
} catch (error) {
response.sendStatus(500);
}
response.sendStatus(200);
});
module.exports = router;