-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.js
More file actions
168 lines (149 loc) · 6.01 KB
/
signup.js
File metadata and controls
168 lines (149 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const express = require('express');
const app = express();
const bcrypt = require('bcrypt');
const db = require('./db');
const axios = require('axios');
const CryptoJS = require('crypto-js');
const naver = require('./config').naver;
const fromNumber = require('./config').phoneNumber;
app.use(express.json());
// 사장님용 회원가입
app.post('/', async (req, res) => {
const body = req.body;
const id = body.id;
const password = body.password;
const nickname = body.nickname;
const phone_number = body.phone_number;
const encode_pwd = await bcrypt.hash(password, 10);
try {
const [result] = await db.execute(`INSERT INTO admin_user (admin_id, password, admin_name, mobile_number) VALUES (?, ?, ?, ?)`,
[id, encode_pwd, nickname, phone_number]);
res.send({ id: result.insertId });
console.log(`SIGNUP_SUCCESS :: userID = {${result.insertId}}` );
} catch (e) {
console.log(`SIGNUP_FAILED :: msg = ${e}`);
res.status(500).send({ msg: 'signup error'});
}
})
// id 중복 확인
app.post('/id-check', async (req, res) => {
const body = req.body;
const id = body.id;
let is_valid = true;
try {
const [result, field] = await db.execute(`SELECT * FROM admin_user WHERE admin_id = ?`, [id]);
// id가 이미 존재하면 is_valid 는 false
if (result.length !== 0) is_valid = false;
res.send({ is_valid: is_valid });
console.log(`ID_CHECK_SUCCESS`);
} catch (e) {
res.status(500).send({ msg: 'server error' });
console.log(`ID_CHECK_FAILED :: msg = ${e}`);
}
})
// sms 인증
function makeSignature(time) {
var space = " "; // one space
var newLine = "\n"; // new line
var method = "POST"; // method
var url = `/sms/v2/services/${naver.id}/messages`; // url (include query string)
var timestamp = time; // current timestamp (epoch)
var accessKey = naver.access; // access key id (from portal or Sub Account)
var secretKey = naver.console_secret; // secret key (from portal or Sub Account)
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secretKey);
hmac.update(method);
hmac.update(space);
hmac.update(url);
hmac.update(newLine);
hmac.update(timestamp);
hmac.update(newLine);
hmac.update(accessKey);
var hash = hmac.finalize();
return hash.toString(CryptoJS.enc.Base64);
}
app.post('/phone-check', async (req, res) => {
const body = req.body;
const phone_number = body.phone_number;
const sms_url = `https://sens.apigw.ntruss.com/sms/v2/services/${naver.id}/messages`;
const time_stamp = Date.now().toString();
const signature = makeSignature(time_stamp);
let code = '';
for (let i = 0; i < 6; i++) code += Math.floor(Math.random() * 10);
try {
const sms_res = await axios.post(sms_url, {
"type":"SMS",
"from": fromNumber,
"countryCode": "82",
"content":`공동장 인증번호는 [${code}]입니다.`,
"messages":[
{
"to": phone_number,
"content":`공동장 인증번호는 [${code}]입니다.`
}
]
}, {
headers: {
'Content-Type': 'application/json; charset=utf-8',
'x-ncp-apigw-timestamp': time_stamp,
'x-ncp-iam-access-key': naver.access,
'x-ncp-apigw-signature-v2': signature
}
}
)
const [result] = await db.execute(`INSERT INTO sms_validation(phone_number, code, expire)
VALUES (?, ?, NOW() + INTERVAL 3 MINUTE) ON DUPLICATE KEY
UPDATE code = ?, expire = NOW() + INTERVAL 3 MINUTE`,
[phone_number, code, code]);
res.send({ msg: 'success' });
console.log(`SMS_SEND_SUCCESS :: phoneNumber = ${phone_number}`);
} catch (e) {
console.log(`SMS_SEND_FAILED :: msg = ${e}`);
res.status(500).send({ msg: 'server error' });
}
})
app.post('/phone-check/verify', async (req, res) => {
const body = req.body;
const code = body.code;
const phone_number = body.phone_number;
let phone_valid = false;
try {
const [result, field] = await db.execute(`SELECT *
FROM sms_validation
WHERE phone_number = ?`, [phone_number]);
const expire_time = new Date(result[0].expire).setHours(result[0].expire.getHours() + 9);
const now = Date.now();
console.log(expire_time, now)
if (code === result[0].code && expire_time > now) {
phone_valid = true;
}
res.send({phone_valid: phone_valid});
console.log(`SMS_VERIFY_SUCCESS :: phoneNumber = ${phone_number}`);
} catch (e) {
console.log(`SMS_VERIFY_FAILED :: msg = ${e}`);
res.status(500).send({ msg: 'server error' });
}
})
app.post('/unique-number', async (req, res) => {
const body = req.body;
const storeName = body.storeName;
const uniqueNumber = body.uniqueNumber;
try {
const [result, field] = await db.execute(`SELECT store_number FROM store WHERE store_name= ?`, [storeName]);
if (result.length === 0) {
res.status(400).send({msg: 'STORE_NOT_FOUND'});
console.log(`STORE_NOT_FOUND :: storeName = ${storeName}`);
} else if (uniqueNumber === result[0].store_number) {
console.log(`UNIQUE_VERIFY_SUCCESS :: storeName = ${storeName}`);
res.send({
msg: 'UNIQUE_NUMBER_VERIFY_SUCCESS',
});
} else {
console.log(`UNIQUE_VERIFY_FAILED :: storeName = ${storeName}`);
res.status(400).send({msg: 'UNIQUE_NUMBER_VERIFY_FAIL'});
}
} catch (e) {
console.log(`UNIQUE_VERIFY_FAILED :: msg = ${e}`);
res.status(500).send({msg: 'SERVER_ERROR'});
}
})
module.exports = app;