-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
125 lines (105 loc) · 3.76 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
const express = require('express');
const app = express();
const port = 3000;
const cors = require('cors');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
const registrationData = [];
const otpData = {};
app.post('/api/register', (req, res) => {
const { first, last, gender, email, mobile, user, pass } = req.body;
console.log('Received data:', req.body);
registrationData.push({ first, last, gender, email, mobile, user, pass, otp: null });
res.json({ message: 'Registration data saved successfully', user: { first, last, gender, email, mobile, user, pass } });
});
app.get('/api/register', (req, res) => {
res.json(registrationData);
});
app.post('/api/check-email', (req, res) => {
const { email } = req.body;
const user = registrationData.find((u) => u.email === email);
res.json({ exists: user !== undefined });
});
app.post('/api/send-otp', (req, res) => {
const { email } = req.body;
const user = registrationData.find((u) => u.email === email);
if (user) {
const otp = generateOTP();
otpData[email] = otp;
sendOTP(email, otp)
.then(() => {
res.json({ success: true, message: 'OTP sent successfully' });
})
.catch((error) => {
res.json({ success: false, message: 'Failed to send OTP' });
});
} else {
res.json({ success: false, message: 'Email not found' });
}
});
app.post('/api/verify-otp', (req, res) => {
const { email, otp } = req.body;
if (otpData.hasOwnProperty(email) && otpData[email] == otp) {
delete otpData[email];
res.json({ success: true, message: 'OTP is correct. Redirecting to password reset.' });
} else {
res.json({ success: false, message: 'Invalid OTP' });
}
});
function generateOTP() {
return Math.floor(1000 + Math.random() * 9000); // Generate a 4-digit OTP
}
function sendOTP(email, otp) {
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]', // Your Gmail email address
pass: 'jpgf nkcq qlgo akpo', // Use the App Password here
},
});
const mailOptions = {
from: '[email protected]', // Your Gmail email address
to: email,
subject: 'OTP Verification',
text: `Your OTP for verification is: ${otp}`,
};
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error('Error sending OTP:', error);
reject(error);
} else {
console.log('OTP sent:', info.response);
resolve();
}
});
});
}
app.post('/api/login', (req, res) => {
const { user, pass } = req.body;
checkUserCredentials(user, pass, res);
});
function checkUserCredentials(username, password, res) {
const user = registrationData.find((u) => u.user === username);
if (user && user.pass === password) {
res.json({ success: true, message: 'Login successful' });
} else {
res.json({ success: false, message: 'Invalid username or password' });
}
}
app.post('/api/update-password', (req, res) => {
const { email, newPassword } = req.body;
const user = registrationData.find((u) => u.email === email);
if (user) {
user.pass = newPassword; // Update the password
res.json({ success: true, message: 'Password updated successfully' });
} else {
res.json({ success: false, message: 'User not found' });
}
});
app.listen(port, () => {
console.log(`API listening on port ${port}`);
});