-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
95 lines (77 loc) · 2.66 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
//jshint esversion:6
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const http = require('http');
const path = require("path");
const users = require('./data').storeDB;
// const dotenv = require('dotenv');
const app = express();
const server = http.createServer(app);
const PORT = process.env.PORT || 1995;
const SECRET_KEY = "1e4cF#Vj8Lp$6Gx!Zo0Wd7Bn@k9mXq3e";
app.use(express.static(path.join(__dirname,'./public')));
// dotenv.config({ path: './.env'});
app.use(express.static(publicDir))
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.json());
// Connect to the MongoDB database
mongoose.connect('mongodb://0.0.0.0:27017/StoreDB', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Define a schema for your data
const storeSchema = new mongoose.Schema({
username: { type: String, required: true, trim: true },
email: { type: String, required: true, trim: true },
password: { type: String, required: true, trim: true },
});
// Create a model based on the schema
const Store = mongoose.model('Store', storeSchema);
app.get("/", (req, res) => {
res.render("index")
})
app.post("/register", (req, res) => {
const { username, email, password } = req.body;
bcrypt
.hash(password, 5)
.then(hashedPassword => {
const newStore = new Store({ username, email, password: hashedPassword });
return newStore.save();
})
.then(() => {
res.status(201).json({ message: "User registered successfully" });
})
.catch(error => {
console.error(error);
res.status(500).json({ message: "An error occurred" });
});
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
Store.findOne({ username })
.then(store => {
if (!store) {
res.status(401).json({ message: "User not found" });
return;
}
return bcrypt.compare(password, store.password);
})
.then(passwordMatch => {
if (!passwordMatch) {
res.status(401).json({ message: "Invalid password" });
return;
}
const token = jwt.sign({ username }, SECRET_KEY);
res.status(200).json({ token });
})
.catch(error => {
console.error(error);
res.status(500).json({ message: "An error occurred" });
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});