-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
78 lines (68 loc) · 1.88 KB
/
server.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
require('dotenv').config(); // Load environment variables
const express = require('express');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(cors());
const otpStorage = {}; // Simple in-memory storage for OTPs
console.log('EMAIL_USER:', process.env.EMAIL_USER);
console.log('EMAIL_PASS:', process.env.EMAIL_PASS);
// Nodemailer configuration
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "[email protected]",
pass: "ruik luhd hzlz pfud",
},
});
const generateOtp = () => {
return Math.floor(100000 + Math.random() * 900000).toString();
};
app.post('/send-otp', (req, res) => {
const {
email
} = req.body;
const otp = generateOtp();
otpStorage[email] = otp;
const mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: 'Your OTP Code',
text: `Your OTP code is ${otp}`,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log('Error sending email:', error);
return res.status(500).json({
success: false,
error: error.message
});
} else {
console.log('Email sent: ' + info.response);
return res.status(200).json({
success: true
});
}
});
});
app.post('/verify-otp', (req, res) => {
const {
email,
otp
} = req.body;
if (otpStorage[email] === otp) {
return res.status(200).json({
success: true
});
} else {
return res.status(400).json({
success: false
});
}
});
app.listen(port, () => {
console.log(`Server running at http://192.168.29.32:${port}`);
});