-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (81 loc) · 2.74 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
const express=require('express');
var createError = require('http-errors');
const path=require('path');
var cookieParser = require('cookie-parser');
var expresshbs=require('express-handlebars');
var hbs=require('hbs');
const authUtils = require('./utils/auth');
//passport starts
const passport = require('passport');
const Strategy = require('passport-local').Strategy;
var session = require("express-session");
const flash = require('connect-flash');
var homeconroller=require('./controller/home.js');
var employeeconroller=require('./controller/employee.js');
var authcontroller=require('./controller/auth.js')
var userscontroller=require('./controller/users.js')
const app=express();
var mongojs = require('mongojs');
app.locals.db = mongojs("mongodb+srv://amol:[email protected]/test?retryWrites=true&w=majority");
const users = app.locals.db.collection('users');
// --------------------------------------------------
passport.use(new Strategy((username, password, done)=> {
users.findOne({ username }, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (user.password != authUtils.hashPassword(password)) {
return done(null, false);
}
return done(null, user);
});
}
));
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
done(null, { id });
});
app.set('views',path.join(__dirname,'/views'));
app.engine('hbs',expresshbs({extname:'hbs',defaultLayout:'mainlayout',layoutsDir:__dirname+"/views/layouts/"}));
app.set('view engine','hbs');
hbs.registerPartials(path.join(__dirname, 'views/partials'));
hbs.registerHelper('ifCond', function(v1, v2, options) {
if(v1 === v2) {
return options.fn(this);
}
return options.inverse(this);
});
const bodyparser=require('body-parser');
app.use(bodyparser.json())
app.use(cookieParser());
app.use(express.static("public"));
// app.use(require('cookie-parser')());
app.use(bodyparser.urlencoded({
extended:true
}));
app.use(session({resave:true,saveUnInitialized:true,secret: "session secret" }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
res.locals.loggedIn = req.isAuthenticated();
next();
});
app.use('/',homeconroller);
app.use('/auth',authcontroller);
app.use('/emp',employeeconroller);
app.use('/users',userscontroller);
// catch 404 and forward to error handler
app.use(function(req, res, next){
res.status(404);
res.send('404: File Not Found');
});
const port=process.env.PORT || 5500;
app.listen(port,()=>{
console.log(`app listening on port ${port}`)
});