-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
291 lines (246 loc) · 7.56 KB
/
app.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const path = require('path');
const ejs = require('ejs');
const express = require('express');
const app = express();
const port = 8080;
var url = require('url');
const session = require('express-session');
const crypto = require('crypto');
const FileStore = require('session-file-store')(session);
const cookieParser = require('cookie-parser');
var bcrypt = require("bcrypt");
var nodemailer = require("nodemailer");
require("dotenv").config();
const swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('./swagger/ swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
app.use(express.static("assets"));
app.use(express.json());
const mysql = require('mysql');
const { get } = require('http');
const con = mysql.createConnection({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USERNAME ,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
});
app.engine('ejs', require('ejs').__express)
con.connect(function (err) {
if (err) throw err;
console.log('Connected');
});
app.use(session({
secret: 'mykey',
resave: false,
saveUninitialized: true,
store: new FileStore()
}));
app.get('/', (req, res) => {
res.send('Hello Inframe!!');
});
//login
app.get('/login', (req, res) => {
res.send({msg:"로그인 페이지"});
});
app.post('/login', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
const body = req.body;
const email = body.email;
const password = body.password;
if (email && password){
con.query('select * from users where email =?', [email], (err, data) => {
if(!data[0]){
res.send({msg:"존재하지 않는 사용자 입니다."});
return;
}
const comparePassword = bcrypt.compareSync(password, data[0].password);
console.log(data[0].password);
console.log(password);
if (email == data[0].email && comparePassword) {
res.send({msg:"로그인 성공!"});
req.session.email =data[0].email;
} else {
res.send({msg:"비밀번호가 잘못되었습니다."});
}
});
}else {
res.send({ msg: "불완전한 데이터" });
}
});
//signup
app.get('/signup', (req, res) => {
res.send({msg:"회원가입 페이지"});
});
app.post('/signup', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
const body = req.body;
const email = body.email;
const password = body.password;
const encryptedPassowrd = bcrypt.hashSync(password, 10);
con.query('select * from users where email=?', [email], (err, data) => {
if (data) {
// 이메일 인증번호 보내기
var generateRandom = function (min, max) {
var ranNum = Math.floor(Math.random()*(max-min+1)) + min;
return ranNum;
}
const number = generateRandom(111111,999999);
let transport = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.EMAIL_PW,
},
});
// email 내용
let mailOptions = {
from: process.env.EMAIL,
to: req.body.email,
subject: "[InFrame] 회원가입 이메일 확인 절차입니다.",
html: "<p>아래 인증번호를 확인하고 입력해주세요!</p>" + number,
};
// email 전송
transport.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
return;
}
console.log(info);
res.send({ msg : "send mail success!" });
});
res.send({msg:"코드 보내기 성공!!"});
con.query('insert into users(email, password, code) values(?,?,?)', [email, encryptedPassowrd,number]);
req.session.email = email;
}
else {
res.send({ msg: "불완전한 데이터" });
}
});
});
// signupCode
app.get('/signupCode', (req, res) => {
res.send({msg:"회원가입 코드 확인 페이지"});
});
app.post('/signupCode/:email', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
const code = req.body.code;
const email = req.params.email;
if (
req.body.code
){
con.query('select * from users where email =?', [email], (err, data) => {
if (code == data[0].code) {
res.send({msg:"회원가입 성공!"});
} else {
res.send({msg:"잘못된 코드입니다!"});
}
});
}else {
res.send({ msg: "불완전한 데이터" });
}
});
// findPassword
app.get('/findPassword', (req, res) => {
res.send({msg:"비밀번호 찾기 페이지"});
});
app.post('/findPassword', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
const email = req.body.email;
if(
email
){
con.query('select * from users where email=?', [email], (err, data) => {
console.log(data[0].email);
// 이메일 인증번호 보내기
var generateRandom = function (min, max) {
var ranNum = Math.floor(Math.random()*(max-min+1)) + min;
return ranNum;
}
const number = generateRandom(111111,999999);
let transport = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.EMAIL_PW,
},
});
// email 내용
let mailOptions = {
from: process.env.EMAIL,
to: req.body.email,
subject: "[InFrame] 비밀번호 찾기를 위한 인증코드입니다.",
html: "<p>아래 인증번호를 확인하고 입력해주세요!</p>" + number,
};
// email 전송
transport.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
return;
}
console.log(info);
res.send({ msg : "send mail success!" });
});
var sql= 'UPDATE users SET code=? WHERE email=?';
con.query(sql, [number,email], function(err, result, fields) {
if(err){
console.log(err);
} else {
res.send({msg:"코드 보내기 성공!!"});
req.session.email = email;
}
});
});}else{
res.send({ msg: "불완전한 데이터" });
}
});
// passwordCode
app.get('/passwordCode', (req, res) => {
res.send({msg:"비밀번호 코드 확인 페이지"});
});
app.post('/passwordCode/:email', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
const code = req.body.code;
const email = req.params.email;
if (
req.body.code
){
con.query('select * from users where email =?', [email], (err, data) => {
if (code == data[0].code) {
res.send({msg:"코드 확인 성공!"});
} else {
res.send({msg:"잘못된 코드입니다!"});
}
});
}else {
res.send({ msg: "불완전한 데이터" });
}
});
// newPassword
app.get('/newPassword', (req, res) => {
res.send({msg:"새로운 비밀번호 설정 페이지"});
});
app.post('/newPassword/:email', (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
const password = req.body.password;
const encryptedPassowrd = bcrypt.hashSync(password, 10);
const email = req.params.email;
var sql= ' UPDATE users SET password = ? WHERE email = ? ';
con.query(sql, [encryptedPassowrd,email], function(err, result, fields) {
if(err){
console.log(err);
} else {
res.send({msg:"비밀번호 변경 성공!!"});
}
});
});
app.get('/logout', (req, res) => {
res.send({ msg: "로그아웃" });
req.session.destroy(function (err) {
res.redirect('/');
});
});
app.listen(port, () => {
console.log(`${port}번 포트에서 서버 대기 중입니다.`);
})