-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
153 lines (126 loc) · 3.73 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
//Carregando Módulos
const express = require('express');
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const mongoose = require("mongoose");
const app = express();
const admin = require("./routes/admin");
const employee = require("./routes/employee");
const client = require("./routes/client");
const path = require("path");
const flash = require("connect-flash");
const session = require("express-session");
const passport = require("passport")
require("./config/auth")(passport);
require("./models/ClienteNovo");
const Client = mongoose.model("clientes");
// CONFIGURAÇÕES
// Sessão
app.use(session({
secret: "Uma barberada",
resave: true,
saveUninitialized: true
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
//Middleware
app.use((req, res, next) => {
res.locals.success_msg = req.flash("success_msg")
res.locals.error_msg = req.flash("error_msg")
res.locals.error = req.flash("error")
res.locals.user = req.user || null
next()
})
//Body Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Handlebars
const hbs = handlebars.create({
defaultLayout: 'main',
runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: true,
},
//Criando um helper handlebars customizável
helpers: {
nivel: function (user) {
return user.nivel
},
ifCond: function (a, b, opts) {
if (a == b)
return opts.fn(this);
else
return opts.inverse(this);
},
now: function () {
let data = new Date();
let dataFormatada = ((data.getDate())) + "/" + ((data.getMonth() + 1)) + "/" + data.getFullYear();;
return dataFormatada;
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
//Mongoose
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/db_bsm", {
useNewUrlParser: true, useUnifiedTopology: true
}).then(function () {
console.log("BaberShop Manager conectado ao banco de dados mongo.")
}).catch(function (err) {
console.log("Houve um erro ao conectar o BarberShop Manager ao bando de dados mongo, erro: " + err);
});
//Public
app.use(express.static(path.join(__dirname, "public")))
//ROTAS
//Rota Principal
app.get('/', (req, res) => {
res.render("index")
});
//Cadatro do Cliente
app.get('/cadastro', (req, res) => {
res.render("cadastro-cliente")
});
//Login dos Usuários
app.get('/login', (req, res) => {
res.render("login")
});
// app.post('/login', (req, res, next) => {
// passport.authenticate(["local-user","local-func"], {
// successRedirect: "/cliente",
// failureRedirect: "/login",
// failureFlash: true
// })(req, res, next)
// })
app.post('/login',
passport.authenticate(['local-func', 'local-user', 'local-admin'], {
failureRedirect: '/login',
failureFlash: true
}),
function (req, res, next) {
if (req.user.nivel == 0) {
res.redirect('/admin');
} else if (req.user.nivel == 1) {
res.redirect('/funcionario');
} else if (req.user.nivel == 2) {
res.redirect('/cliente');
}
next();
})
app.get("/logout", (req, res) => {
req.session.destroy((err) => {
res.redirect('/')
})
})
//Rotas do Adminstrador
app.use('/admin', admin);
//Rotas do Funcionários
app.use('/funcionario', employee);
//Rotas do Cliente
app.use('/cliente', client);
//OUTRAS CONFIGURAÇÕES
const PORT = 3308;
app.listen(PORT, () => {
console.log(`BarberShop Manager rodando no seguinte servidor local: http://localhost:${PORT}`)
})