-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
144 lines (112 loc) · 3.25 KB
/
Copy pathapp.js
File metadata and controls
144 lines (112 loc) · 3.25 KB
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
import express from "express";
import mongoose from "mongoose";
import bcrypt from "bcrypt";
import User from "./models/User.js";
import jwt from "jsonwebtoken";
import cookieParser from "cookie-parser";
const SECRET_KEY = "mysecretkey";
const app = express();
const port = 3000;
mongoose.connect("mongodb://localhost:27017/AuthDB")
.then(() => {
console.log("MongoDB Connected");
})
.catch((err) => {
console.log(err);
});
// Tell Express to use EJS
app.set("view engine", "ejs");
// Public folder for CSS
app.use(express.static("public"));
// Middleware to parse form data
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// Routes
app.get("/signup", (req, res) => {
res.render("signup");
});
app.get("/login", (req, res) => {
res.render("login", {
error: null
});
});
app.get("/dashboard", (req, res) => { //still not protected anyone can access it
//to protect it we need to check if the user is authenticated before rendering the dashboard
const token = req.cookies.token;
if(!token){
return res.redirect("/login");
}
try {
const decoded = jwt.verify(
token , SECRET_KEY
);
// console.log(decoded);
res.render("dashboard", {
username : decoded.username //sends username to dashboard to display it
});
}
catch (err){
res.redirect("/login");
}
});
app.get("/", (req, res) => {
res.redirect("/login");
});
app.post("/signup", async (req, res) => {
try {
const { username, password } = req.body;
console.log(req.body);
const hashedPassword = await bcrypt.hash(password, 10);
const user = new User({
username,
password: hashedPassword
});
await user.save();
res.send("User registered successfully!");
} catch (err) {
console.log(err);
res.send("Error registering user.");
}
});
app.post("/login", async (req, res) => {
try{
const { username , password } = req.body;
const user = await User.findOne({username});
if(!user){
// return res.send("User not found");
return res.render("login", {
error: "User not found"
});
}
const isMatch = await bcrypt.compare(
password,
user.password
);
if(!isMatch){
// return res.send("Password Invalid");
return res.render("login", {
error: "Invalid password"
});
}
const token = jwt.sign(
{
username : user.username
},
SECRET_KEY
);
// console.log(token);
res.cookie("token", token); //Browser stores: token in cookies
res.redirect("/dashboard");
}
catch (err){
console.log(err);
res.send("Login Error");
}
});
app.get("/logout", (req, res) => {
res.clearCookie("token");
res.redirect("/login");
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});