-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
112 lines (94 loc) · 3.36 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
const express = require('express');
// const session = require('express-session');
// const MongoStore = require('connect-mongo')(session); // Se usa par aguardar las sessiones en mongodb ya que las sessiones por defecto se guardan en memoria del server.
const bodyParser = require('body-parser');
const passport = require('passport');
const auth = require('./auth');
// const Connect = require('./conecction.js');
const userCtrl = require('./controllers/user');
const middleSession = require('./middlewares/session');
const jwt = require('express-jwt');
const config = require('./config');
// require('dotenv').config();
// const db = new Connect();
// const conn = db.connect();
const app = express();
/* // Esto es un middleware -> La función se ejecuta cada vez que la aplicación recibe una solicitud.
app.use(session({
secret: process.env.SESSION_SECRET || 'some-secret',
// resave: true, investigar mas -> https://www.npmjs.com/package/express-session
resave: false,
// saveUninitialized: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: conn,
})
})); */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(bodyParser.urlencoded({ extended: false }));
app.set('view engine', 'pug');
// app.set('views', `${__dirname}/views`)
app.use(middleSession);
app.use(passport.initialize());
app.use(passport.session());
passport.use(auth.LocalStrategy);
passport.use(auth.FacebookStrategy);
passport.deserializeUser(auth.deserializeUser);
passport.serializeUser(auth.serializeUser);
app.get('/', (req, res) => {
// console.log(req.session)
// console.info(req.user)
// Cuenta es el nombre que le damos y lo agregamos al object session
req.session.cuenta = req.session.cuenta ? req.session.cuenta + 1 : 1
res.status(200).send(`Hola has visto esta página ${req.session.cuenta}`)
// res.status(200).send('ok bien')
})
app.get('/login/facebook', (req, res) => {
res.render('index', { title: 'Hey', message: 'Hello there!'});
})
app.get('/user', auth.isAuthenticate, (req, res) => {
// console.log(req.session.token)
res.render('user', { user: req.user, token: req.session.token })
})
app.post('/signup', userCtrl.postSignup);
app.post('/login', userCtrl.postLogin);
app.get('/login', (req, res) => {
res.render('login');
})
app.get('/logout', auth.isAuthenticate, userCtrl.logout); // Si esta autenticado, si haga el logout
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email'] } ));
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
successRedirect: '/user',
failureRedirect: '/login/facebook'
}));
app.get('/user/info', auth.isAuthenticate, (req, res) => {
res.json(req.user); // Gracias a passport no devuelve un req.user con toda la info del user
});
app.get('/verify', auth.isAuthenticate, (req, res) => {
// console.log(req.session)
auth.verify(req.session.token, config.secret, () => {
res.status(200).send('sucess')
})
})
/*
Payload
{
"permissions":{
"metrics":"read"
},
"username":"jandrey15",
"admin":true,
"iat":1516239022
}
*/
app.get('/protected', jwt({ secret: config.secret }), (req, res) => {
const { user } = req;
if (!user || !user.name) {
return res.status(404).send('Not authorized')
}
// console.log(user)
// if (!req.user.admin) return res.sendStatus(401);
res.status(200).send(user)
})
module.exports = app;