This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
100 lines (84 loc) · 2.41 KB
/
app.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
"use strict";
const express = require('express');
const app = express();
const session = require("express-session");
const MongoStore = require('connect-mongo')(session);
const http = require("http").Server(app);
const io = require('socket.io')(http);
const socket = require("./module/socket")(io);
const bodyParser = require("body-parser");
const bcrypt = require("bcryptjs");
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const morgan = require('morgan');
const flash = require("connect-flash");
const { ExpressPeerServer } = require('peer');
// DB Connect
const mongoose = require("./db");
const options = {
debug: true,
path: '/peerserver'
}
const peerserver = ExpressPeerServer(http, options);
app.use('/peer', peerserver);
app.use(express.static("public"));
app.set("view engine", "ejs");
app.set("views", "./views");
app.use(flash());
//app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret: "daylapassword",
resave: false,
saveUninitialized: false,
//store: new FileStore(),
store: new MongoStore({ url: mongoose.dbUrl }),
cookie: {
maxAge: 31536000000,
}
}));
app.use(passport.initialize());
app.use(passport.session());
http.listen(8000, () => {
console.log("Server Running On Port: 8000");
});
const routers = require("./module/routes")(app, passport,io);
//Passport connect
const User = require("./schema/user");
passport.use(new LocalStrategy({
passReqToCallback: true
}, (req, username, password, done) => {
User.findOne({ username: username }, function (err, user) {
if (err) {
return done(err);
} else if (!user) {
console.log("Incorrect Username ", username);
return done(null, false, req.flash('loginMessage', 'No user found.'));
}
bcrypt.compare(password, user.password, async (err, result) => {
if (result === true) {
let a = await User.findOneAndUpdate({
username: user.username
}, {
is_online: 'true'
})
io.of('/user').emit('user-online',user._id)
req.session.save();
return done(null, user);
} else {
console.log("Incorrect password");
return done(null, false, req.flash('loginMessage', 'Wrong Password !'));
}
})
});
}
));
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});