-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
148 lines (108 loc) · 4.64 KB
/
connect.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 bcrypt = require('bcrypt');
const { MongoClient } = require('mongodb');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = 3000;
const salt = bcrypt.genSaltSync(15); // 10 salt rounds for password hashing
// setting up sessions, to tell when user is logged in or not
const session = require('express-session');
// Set up sessions
app.use(session({
secret: 'secret_key',
resave: false,
saveUninitialized: true,
}));
// Host each file inside public folder
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
return res.redirect('/index.html');
});
app.get('/index.html', function (req, res) {
return res.sendFile(path.join(__dirname, 'public', '/index.html'));
});
app.get('/forecast.html', function (req, res) {
return res.sendFile(path.join(__dirname, 'public', '/forecast.html'));
});
app.get('/weatherMap.html', function (req, res) {
return res.sendFile(path.join(__dirname, 'public', '/weatherMap.html'));
});
app.get('/login.html', function (req, res) {
return res.sendFile(path.join(__dirname, 'public', '/login.html'));
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
// Connect to the MongoDB cluster
//Password = TKVUNihz8KazgTGS
const uri = "mongodb+srv://dsteele1906:[email protected]/?retryWrites=true&w=majority";
const client = new MongoClient(uri);
async function connectToMongoDB() {
try {
await client.connect();
console.log("Connected to MongoDB");
return client.db("WeatherApplication");
} catch (error) {
console.error("Error connecting to MongoDB:", error);
throw error;
}
}
// signup form - will log the users username, password and email to the mongo users collection
app.post('/signin', async (req, res) => {
try {
const db = await connectToMongoDB();
const { username, password } = req.body;
const usersCollection = db.collection('users');
// checks for existing account with the same name to prevent duplicate entries
const existingAccount = await usersCollection.findOne({ username: username });
if (existingAccount) {
console.log(`User ${username} already exists, please try again`);
return res.redirect('/login.html?error=' + encodeURIComponent('Username already exists, please try again'));
}
const hash = await bcrypt.hash(password, salt); // salt and hash the password before storing in database.
const insertResult = await usersCollection.insertOne({
username: username,
password: hash,
});
console.log(insertResult);
console.log(`User ${username} registered successfully!`);
return res.redirect('/login.html?success=' + encodeURIComponent('Signup complete! Please log in to access your account.')); // Redirect to login.html
} catch (error) {
console.error("Error in signup route:", error);
return res.redirect('/login.html?error=' + encodeURIComponent('An error occurred. Username is not unique, please try again'));
}
});
// login form , checks against the users collection to see if username and password matches with existing accounts.
app.post('/login', async (req, res) => {
try {
const db = await connectToMongoDB();
const { username, password } = req.body;
const usersCollection = db.collection('users');
const user = await usersCollection.findOne({ username: username });
if (user) {
const isValid = await bcrypt.compare(password, user.password);
if (isValid) {
req.session.user = user; // Save the user data in the session
return res.redirect('/weatherMap.html');
}
else {
return res.redirect('/login.html?error=' + encodeURIComponent('Incorrect Password, Please try again'));
}
} else {
return res.redirect('/login.html?error=' + encodeURIComponent('Username incorrect, Please try again'));
}
} catch (error) {
console.error("Error in login route:", error);
return res.status(500).send('Internal Server Error');
}
});
// logout route, destroys the session and redirects to login.html
app.get('/logout', (req, res) => {
req.session.destroy();
return res.redirect('/login.html');
});
// delete route
connectToMongoDB().catch(console.error);
});