-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (88 loc) · 2.58 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
const express = require("express");
const app = express();
const path = require("path");
const mongoose = require("mongoose");
const User = require("./models/user.js");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser");
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
async function main() {
await mongoose.connect("mongodb://localhost:27017/auth-test");
}
main().then(() => console.log("Connected")).catch((err) => console.log(err));
app.get("/",checkToken, (req, res) => {
res.send("Home");
});
app.get("/signup", (req, res) => {
res.render("signup");
});
app.post("/signup", async (req, res) => {
let { email, username, password } = req.body;
const user = await User.findOne({ email });
if (user) {
return res.send("User already exists");
} else {
const hashPassword = await bcrypt.hash(password, 10);
const user = await User.create({
email: email,
password: hashPassword,
username: username
});
console.log("Signup successful");
const payload = {
id:user.id,
email:user.email
}
const token = generateToken(payload)
res.cookie('token', token, { httpOnly: true });
res.redirect("/login");
}
});
app.get("/login", (req, res) => {
res.render("login");
});
app.post("/login", async (req, res) => {
let { email, password } = req.body;
const user = await User.findOne({ email: email });
if (user) {
const passcode = await bcrypt.compare(password, user.password);
if (passcode) {
const payload = {
id:user.id,
email:user.email
}
const token = generateToken(payload)
res.cookie('token', token, { httpOnly: true });
return res.redirect("/");
} else {
return res.send("Password wrong");
}
} else {
return res.send("User not found");
}
});
//Functions
function generateToken(userData){
return jwt.sign(userData,"secret")
}
function checkToken(req,res,next){
const token = req.cookies.token
if(!token){
res.redirect("/login")
}
try {
const decode = jwt.verify(token,"secret")
console.log(decode)
req.user = decode
next()
} catch (error) {
console.log(error)
}
}
app.listen(3000, () => {
console.log("Server listening on port 3000");
});