-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (129 loc) · 5.21 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
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
const express = require('express');
const cors = require('cors');
const bcrypt = require('bcrypt');
const saltRounds = 10;
const jwtSecret = "secretPassword";
const jwt = require('jsonwebtoken');
const pool = require('./db.js');
const app = express();
const path = require('path');
var nodemailer = require('nodemailer');
const PORT = process.env.PORT || 5000;
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'linxtechco@gmail.com',
pass: process.env.EMAIL_PASSWORD
}
});
app.use(express.json());
app.use(cors());
app.use(express.static('./client/build'));
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, 'client/build')));
}
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, "client/build/index.html"));
});
//Get folders for user with id
app.get('/folders/:id', async (req, res) => {
const query = `SELECT * FROM folders WHERE u_id = ${req.params.id};`;
const result = await pool.query(query);
res.json(result.rows);
});
//Get Links for folder of id
app.get('/links/:id', async (req, res) => {
const query = `SELECT * FROM links WHERE f_id = ${req.params.id};`;
const result = await pool.query(query);
res.json(result.rows);
});
//Add Folder
app.post('/', async (req, res) => {
const {name, description, u_id} = req.body;
const query = `INSERT INTO folders (name, description, u_id) values ('${name}', '${description}', ${u_id});`;
const result = await pool.query(query);
res.json(result.rows);
});
//Add Link
app.post('/folders/:id', async (req, res) => {
const {name, url, description} = req.body;
const query = `INSERT INTO links (name, url, description, f_id) VALUES ('${name}', '${url}', '${description}', ${req.params.id});`;
const result = await pool.query(query);
res.json(result.rows);
});
//Delete Folders
app.delete('/folders/:id', async (req, res) => {
//Delete Links
const query2 = `DELETE FROM links WHERE f_id = ${req.params.id};`;
await pool.query(query2);
//Delete Folder
const query1 = `DELETE FROM folders WHERE f_id = ${req.params.id};`;
await pool.query(query1);
});
//Signup
app.post('/signup', async (req, res) => {
const {first_name, last_name, email, password} = req.body;
//Check if user already exists
const query = `SELECT * FROM users WHERE email = '${email}';`;
const user = await pool.query(query);
//If user already exists with that email
if (user.rowCount > 0) {
res.json({ errorMessage: "Sorry, this username is not available. Please choose a new one." });
} else {
bcrypt.hash(password, saltRounds, async (err, hash) => {
//Insert user into database
const query = `INSERT INTO users (first_name, last_name, email, password) values ('${first_name}', '${last_name}', '${email}', '${hash}');`;
const result = await pool.query(query);
if (result.rows) {
res.json({ message: "Registration was successful!" });
//Sends welcome email to user
var mailOptions = {
from: 'linxtechco@gmail.com',
to: email,
subject: 'Welcome to Linx',
html: `<p>Hey ${first_name},<br><br>Thank you for joining the Linx family! Your life might be a mess, but your linx just got a whole lot more organized. Don't foget to link up with us on social media.<br><br><a href="https://linxtech.herokuapp.com">Linx</a></p>`,
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
} else {
res.json({ errorMessage: "Registration failed." });
}
});
}
});
//Signin
app.post('/signin', async (req, res) => {
const {email, password} = req.body;
//Get hashPassword from database
const query = `SELECT password FROM users WHERE email = '${email}';`;
const result = await pool.query(query);
const hashPassword = result.rows.length > 0 ? result.rows[0].password : null;
if (hashPassword) {
bcrypt.compare(password, hashPassword, async function(err, result) {
if (result) {
//Get user info and return it with a jwt
const query2 = `SELECT * FROM users WHERE email = '${email}';`;
const result2 = await pool.query(query2);
const userInfo = result2.rows[0];
const { u_id, first_name, last_name} = userInfo;
const token = jwt.sign({ u_id, first_name, last_name, email}, jwtSecret);
res.json( {user: {u_id, first_name, last_name, email}, token: token });
} else {
res.json({ message: "Wrong password. Please try again." });
}
});
} else {
res.json({ message: "No user exists with that email." })
}
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client/build/index.html"));
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
});