Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
212 lines (186 loc) · 5.46 KB
/
Copy pathserver.js
File metadata and controls
212 lines (186 loc) · 5.46 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
//Bibliotecas Instaladas
const express = require("express");
const app = express();
const bcrypt = require("bcrypt");
const passport = require("passport");
const initializePassport = require("./passport-config");
const flash = require("express-flash");
const session = require("express-session");
const methodOverride = require("method-override");
const Sequelize = require("sequelize");
const Post = require("./models/pedidos");
initializePassport(
passport,
(email) => users.find((user) => user.email === email),
(id) => users.find((user) => user.id === id)
);
const users = [];
app.use("/public", express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
is_logged_in: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
//Processo de Login
app.post(
"/login",
checkNotAuthenticated,
passport.authenticate("local", {
successRedirect: "/usuario",
failureRedirect: "/login",
failureFlash: true,
})
);
//Processo de Registro
app.post("/cadastro", checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
users.push({
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: hashedPassword,
});
console.log(users);
res.redirect("/login");
} catch (e) {
console.log(e);
res.redirect("/cadastro");
}
});
//Início Rotas
//Rota Visualização
app.get("/posts", checkAuthenticated, function (req, res) {
Post.findAll({ order: [["id", "DESC"]] }).then(function (posts) {
res.render("posts.ejs", { posts: posts });
});
});
//Rota Home Página não restrita
app.get("/home", checkNotAuthenticated, function (req, res) {
Post.findAll({ order: [["id", "DESC"]] }).then(function (posts) {
res.render("home.ejs", { posts: posts });
});
});
//Rota Usuario
app.get("/usuario", checkAuthenticated, (req, res) => {
res.render("usuario.ejs", { name: req.user.name });
});
//Rota orcamento
app.get("/orcamento", checkAuthenticated, (req, res) => {
res.render("orcamento.ejs", { name: req.user.name });
});
//Rota Login
app.get("/login", checkNotAuthenticated, (req, res) => {
res.render("login.ejs");
});
//Rota de cadastro
app.get("/cadastro", checkNotAuthenticated, (req, res) => {
res.render("cadastro.ejs");
});
//Rota de contato
app.get("/contato", checkNotAuthenticated, (req, res) => {
res.render("Contato.ejs");
});
app.get("/contate", checkAuthenticated, (req, res) => {
res.render("contate.ejs");
});
//Rota de index
app.get("/", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/index.html");
});
//Rota sobre
app.get("/sobre", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/sobre.html");
});
//Rota de soluções Especifica
app.get("/biomassa", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/Solucoes-Especificas/Energia-Biomassa.html");
});
app.get("/eolica", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/Solucoes-Especificas/Energia-Eolica.html");
});
app.get("/solar", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/Solucoes-Especificas/Energia-Solar.html");
});
app.get("/servicosEnergia", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/Solucoes-Especificas/Serviços-Energia.html");
});
app.get("/smartGrid", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/Solucoes-Especificas/Smart-Grid.html");
});
app.get("/smartPower", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/Solucoes-Especificas/Smart-Power.html");
});
//Rota Preservação(sustentabilidade)
app.get("/preservacao", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/sustentabilidade.html");
});
app.get("/cromoit", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/cromo-it.html");
});
//Rota confirmado o envio de e-mail
app.get("/confirmado", checkNotAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/confirmarEnvio.html");
});
app.get("/concluido", checkAuthenticated, (req, res) => {
res.sendFile(__dirname + "/src/confirmar.html");
});
//Fim Rotas
//Inserção de Dados na tabela postagem
app.post("/add", function (req, res) {
Post.create({
cidade: req.body.cidade,
estado: req.body.estado,
servico: req.body.servicos,
descricao: req.body.descricao,
})
.then(function () {
res.redirect("/usuario");
})
.catch(function () {
res.send("Houve um erro!");
});
});
//Deletar os dados da tabela
app.get("/deletar/:id", function (req, res) {
Post.destroy({ where: { id: req.params.id } })
.then(function () {
res.redirect("/posts");
})
.catch(function (erro) {
res.send("A Postagem não existe.");
});
});
//Comando para deslogar do sistema
app.delete("/logout", (req, res) => {
req.logout(req.user, (err) => {
if (err) return next(err);
res.redirect("/");
});
});
//Funções de Autenticação
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();
}
//Localhost
app.listen(8081);