-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (109 loc) · 3.69 KB
/
index.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
const express = require('express')
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const cookieParser = require('cookie-parser')
const path = require('path');
require('dotenv').config();
const PORT = process.env.PORT;
const MY_GMAIL = process.env.MY_GMAIL;
const MY_GMAIL_PASSWORD= process.env.MY_GMAIL_PASSWORD;
const uri = process.env.URI;
//see if this works
const app = express()
const port = PORT || 5000;
if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(cookieParser());
const conn = mongoose.createConnection(uri, { useNewUrlParser: true, useUnifiedTopology: true });
conn.on('connected', function() {
console.log('database is connected successfully');
});
conn.on('disconnected',function(){
console.log('database is disconnected successfully');
})
conn.on('error', console.error.bind(console, 'connection error:'));
var hour = 3600000;
app.use(session({secret: 'user_sess', saveUninitialized: false, rolling: true, resave: true, cookie:{maxAge: hour, expires: new Date(Date.now() + hour)}, store: conn})).post('/api/login', (req,res)=>{
const {name , email, password} = req.body;
console.log(req.protocol)
conn.collection('users').findOne({name, email, password}, (err, result)=>{
if(err)
{res.status(404).send(err)}
else if(result){
req.session.save(function(error){
if(error){
console.log(error);
}
})
res.json({message: 'Login successfull', user: result.name});
console.log(req.session);
}else{
res.status(200).send('Login failed');
}
})
}
)
app.post('/api/signup', (req,res)=>{
const {name, email, password} = req.body;
// check if user already exists
conn.collection('users').findOne({name : name, email: email}, (err, user)=>{
if(err) {res.status(404).send(err)}
else if(user){
res.status(200).send("User already exists")
}else{
conn.collection('users').insertOne({name: name, email: email, password: password}, (err, result)=>{
if(err) {res.status(404).send(err)};
res.status(200).send('Signup successfull')
})
}
})
})
app.post('/api/sendMessage', (req, res) => {
const { name, email, message } = req.body
const data = req.body
console.log(req.body)
const transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 465,
auth: {
user: MY_GMAIL,
pass: MY_GMAIL_PASSWORD
}
}));
const mailOptions = {
from: data.email,
to: MY_GMAIL,
subject: `A Message from ${data.email}`,
bcc: data.email,
text: `
Email: ${data.email}
Name: ${data.name}
Message: ${data.message}
`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.status(200).send('Send email successfully');
console.log(res)
});
app.listen(port, () => {
console.log(`Example app listening at ${port}`)
})