-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
116 lines (92 loc) · 2.84 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
111
112
113
114
115
116
const express = require("express")
var cors = require('cors')
var flash = require("connect-flash")
const bodyParser = require("body-parser")
var passport = require("passport")
var LocalStratergy = require("passport-local")
const path = require("path")
const multer = require("multer")
const uuid = require("uuid")
const dotenv = require("dotenv")
dotenv.config()
const connectDB = require("./config/db")
var User = require("./routeModels/user/User")
const app = express()
connectDB()
app.use(cors())
app.use(express.static("public"))
app.use('/uploads',express.static("uploads"))
app.use(bodyParser.urlencoded({ extended : true }))
app.use(bodyParser.json());
app.set("view engine","ejs")
const fileStorage = multer.diskStorage({
destination : "uploads/",
filename : function(req,file,cb){
cb(null,file.fieldname + "-" + uuid.v4() + path.extname(file.originalname));
}
})
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'image/png' ||
file.mimetype === 'image/jpg' ||
file.mimetype === 'image/JPG' ||
file.mimetype === 'image/jpeg'
) {
cb(null, true);
} else {
cb(null, false);
}
};
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single('uri')
);
const middleware = require("./middleware")
const authRoutes = require("./routes/auth")
const indexRoutes = require("./routes/index")
const resetPasswordRoutes = require("./routes/resetPassword")
const authorisedRoutes = require("./routes/authorised")
const indexRouteAuthorised = require("./routeModels/authorised/indexRoute")
app.use(require("express-session")({
resave : false, saveUninitialized : false , secret : "This is cara"
}))
app.use(flash());
app.use(function(req,res,next){
res.locals.error = req.flash("error")
res.locals.success = req.flash("success")
next();
})
app.use(passport.initialize())
app.use(passport.session())
passport.use(new LocalStratergy(User.authenticate()))
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
app.get("/", async (req,res) => {
if(req.user){
if(req.user.isAuthorised){
res.redirect("/authorised-index")
}else{
res.redirect("/index")
}
}else{
res.render("auth/login",{ title : "Login" })
}
} )
app.use(authRoutes)
app.use(indexRoutes)
app.use(resetPasswordRoutes)
app.get("/authorised-index",middleware.isLoggedIn,indexRouteAuthorised)
app.use("/authorised/", authorisedRoutes)
app.get("/resetPassword",(req,res) => {
res.render("reset",{ user : req.user })
} )
app.get("/sessionExpired",function(req,res){
req.flash("error","Sign in to continue!!!")
res.redirect("/")
})
app.get("/*",(req,res) => {
res.render("404error")
} )
const port = process.env.PORT || 3000
app.listen(port,() => {
console.log(`Server at ${port}`)
} )