-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (105 loc) · 3.11 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
103
104
105
106
107
108
109
110
const express = require("express");
const path = require("path");
const register = require("./Routes/Register");
const login = require("./Routes/Login");
const music = require("./Routes/Music");
const cors = require("cors");
const session = require("express-session");
const passport = require("passport");
const multer = require("multer");
const cookieParser = require("cookie-parser");
const {redisClient}=require("./redis");
const csrf = require("csurf");
const dbConfig = require('./config/database.config.js');
const redisStore = require("connect-redis")(session);
const upload = multer();
const app = express();
const port = process.env.PORT || 4000;
app.use(
cors(
process.env.NODE_ENV === "production"
? { credentials: true }
: {
credentials: true,
origin: "http://localhost:3000",
}
)
);
app.use(
session({
secret: "tHiSiSasEcRetStr",
name: "_redisPractice",
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: process.env.NODE_ENV === "production" ? false : true,
maxAge: 60 * 60 * 1000,
},
store: new redisStore({ client: redisClient, ttl: 3600000 }),
})
);
app.use(cookieParser());
app.use(csrf());
app.use(passport.initialize());
app.use(passport.session());
const csrfMiddleware = (req, res, next) => {
res.cookie("XSRF-TOKEN", req.csrfToken());
res.locals.csrfToken = req.csrfToken();
next();
};
app.use(csrfMiddleware);
app.use(function (err, req, res, next) {
if (err.code !== "EBADCSRFTOKEN") return next(err);
res.status(403);
res.send({ err: true, message: "Invalid Csrf Token" });
});
app.use(express.static("frontend/build"));
const authCheck = (req, res, next) => {
if (req.user && req.user.id) {
next();
} else {
res.status(401).json({ message: "UnAuthorized" });
}
};
require("./config/passport")(passport);
app.post(
"/onLogin",
upload.none(),
passport.authenticate("local", {
failureRedirect: "/login/error",
}),
function (req, res) {
res.send({ success: true, id: req.user, message: "Login success" });
}
);
app.get("/login/error", (req, res) => {
res.status(401).json({ message: "Failed to Login" });
});
app.get("/error", (req, res) => {
res.send({ success: false, message: "Invalid Crendentials" });
});
app.get("/login_auth", authCheck, (req, res) => {
if (req.isAuthenticated()) {
res.send({ status: true, message: "user autheticated" });
} else {
res.status(401).send({ status: false, message: "unAuthorised" });
}
});
app.get("/profile", authCheck, (req, res) => {
res.send(req.user);
});
app.use("/register", register);
app.use("/music", authCheck, music);
app.use("/login", authCheck, login);
app.get("/logout", (req, res) => {
// res.clearCookie("_redisPractice", {domain: "127.0.0.1",path:'/'})
//res.clearCookie("XSRF-TOKEN", {expires: new Date(),path:'/'})
// res.cookie("_redisPractice", '', { expires: new Date(), path: '/' })
req.logOut();
//req.session.destroy();
res.send({ success: true, message: "logged out successfully" });
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, `frontend`, `build`, `index.html`));
});
app.listen(port);