-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
168 lines (147 loc) · 4.16 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const express = require('express')
const app = express()
const passport = require('passport');
const flash = require('express-flash');
const session = require('express-session');
const methodOverride = require('method-override');
const ChatUserDB = require('./DB/Tables/Chat-User')
const { AuthDB, MessageDB } = require('./DB/DB_main')
const bcrypt = require('bcrypt')
const sass = require('node-sass-middleware')
const initializePassport = require('./passport-config');
initializePassport(
passport,
FindUserEmail,
AuthDB.getAuthDataId
)
const users = []
app.set("view engine", "ejs")
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(sass({
src: __dirname + '/scss',
dest: __dirname + '/public',
}))
app.use(express.static(__dirname));
app.use(session({
secret: "aaaaaaa",
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'));
/* /////////////////// Router ///////////////////// */
app.get('/', checkAuthenticated, async (req, res) => {
res.redirect("/messenger")
});
app.get("/messenger", async (req, res) => {
let user = await req.user
if (!user) {
res.redirect("/login")
return
}
let chatsArr = await GetChats(user.id)
res.render('messenger.ejs', {
name: user.name,
chats: chatsArr,
id: user.id,
isChat: false,
})
})
app.get('/messenger/:id', async (req, res) => {
let user = await req.user
if (!user) {
res.redirect("/login")
return
}
let chatsArr = await GetChats(user.id)
let chatIndex;
for(let i = 0; i < chatsArr.length; i++) {
if (chatsArr[i].id == req.params.id) {
chatIndex = i;
break;
}
}
res.render('messenger.ejs', {
name: user.name,
chats: chatsArr,
chatIndex: chatIndex,
id: user.id,
isChat: true,
messages: await GetMessages(req.params.id)
})
})
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render('login.ejs', {
test: "test message",
greeting: "Здравствуйте"
})
})
app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}))
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render('register.ejs', {
test: "register",
greeting: "Регистрация"
})
})
app.post('/register', checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
let newUser = {
name: req.body.name,
surname: req.body.surname,
email: req.body.email,
phone: req.body.phone_number,
password: hashedPassword,
invite_code: req.body.code,
};
await AuthDB.register(newUser)
res.redirect('/login')
} catch {
res.redirect('/register')
}
})
app.delete('/logout', (req, res) => {
req.logOut()
res.redirect('/login')
})
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
res.redirect('/login')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/')
}
next()
}
async function FindUserEmail(data) {
let res = await AuthDB.getAuthData({ "data": data })
if (res["stat"] == 'ERR') {
return null
}
console.log(res)
return res["data"]
}
async function GetChats(user_id) {
console.log(user_id)
let chats = []
let userchats = await ChatUserDB.get_user_chats({ "user_id": user_id })
for (let i = 0; i < userchats.length; i++) {
chats.push((await ChatUserDB.get_chat_info({ "chat_id": userchats[i], "user_id": user_id })))
}
return chats
}
async function GetMessages(chat_id) {
let msgs = await MessageDB.get_all_msgs_for_site(chat_id)
return msgs
}
mobile_server = app.listen(process.env.PORT || 3000, () => console.log(`Server is running ${process.env.PORT}`)) //! ТАК СДЕЛАТЬ МИШЕ
require('./WS/ws')