-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
95 lines (77 loc) · 2.16 KB
/
app.ts
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
import express = require('express');
import session = require('express-session');
import passport = require('passport');
import bodyparser = require('body-parser');
import LocalStrategy = require('passport-local');
import SiteRouter = require('./routes/siterouter');
import APIRouter = require('./routes/apirouter');
var app = express();
var tpl = 'social';
app.set('views', 'template/' + tpl);
app.set('view engine', 'jade');
app.use((req, res, next) => {
if (req.query.hasOwnProperty('t')) {
tpl = req.query.t;
app.set('views', 'template/' + tpl);
}
next();
});
passport.use('local', new LocalStrategy(function (email, pass, done) {
console.log('checking for auth:', email);
app.get('models').User
.findOne({where: {email: email, password: pass}})
.then(function (user) {
if (!user)
done(null, false);
else
done(null, user);
});
}));
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
var ALL_ROLES = ['Admin', 'Inspetor', 'Diretor', 'Visitante'];
app.locals = {
pretty: true,
site: {
base: '/',
name: 'Inspetoria Online',
nav: [
{title: 'Escolas', icon: 'graduation-cap', link: '/escolas', roles: ALL_ROLES},
{title: 'Usuários', icon: 'users', link: '/usuarios', roles: ['Admin', 'Inspetor']},
{title: 'Atendimentos', icon: 'calendar', link: '/atendimentos', roles: ALL_ROLES}
]
}
};
app.use(express.static('../insp-frontend'));
app.use(session({
secret: 'huvsnivs is now or never',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/login', function (req, res) {
res.status(200).render('login');
});
app.post('/login',
bodyparser.urlencoded({extended: false}),
(req, res, next) => {
console.log('tentativa de login', req.body);
next()
},
passport.authenticate('local'),
(req, res) => {
res.redirect(req.session.returnTo || '/');
delete req.session.returnTo;
});
app.use(function (req, res, done) {
res.locals.user = req.user;
done();
});
app.use('/api', APIRouter);
app.use(SiteRouter);
export = app;