I'm having problems after user authentication in form. The passport redirects to the root again, however it does not pass to the GET of the '/' route expression and displays [object Object].
If you use
servidor.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Alguma coisa deu errada: \n Detalhes do erro: ' + err.stack);
});
Req is getting the user object I am using to authenticate.
example generated log
GET / 302 5539.955 ms - 56
GET /login 200 4440.040 ms - 1824
POST /login 302 947.376 ms - 46
[object Object]
GET / 500 443.628 ms - 142
server.js
const express = require('express'),
servidor = express(),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
path = require('path'),
passport = require('passport'),
session = require('express-session'),
flash = require('connect-flash'),
log = require('morgan'),
//favicon = require('serve-favicon'),
ambiente = process.env.NODE_ENV || 'dev';
if(ambiente == 'dev'){
servidor.set('port', 3000);
}else if(ambiente == 'pro'){
servidor.set('port', 8080);
}
require('./modelo/autenticacao')(passport);
servidor.set('admin', path.join(__dirname,'public/views/admin'));
servidor.set('views', path.join(__dirname,'public/views'));
servidor.set('view engine', 'html');
servidor.engine('.html', require('ejs').__express);
servidor.use(express.static(path.join(__dirname,'public')));
servidor.use(log(ambiente));
servidor.use(cookieParser());
servidor.use(bodyParser.urlencoded({ extended: true }));
servidor.use(bodyParser.json());
servidor.use(session({
secret: "Ch4v3GPL0G",
resave: true,
saveUninitialized: true,
cookie:{
secure: false,
expires: Date.now() + 12 * 60 * 60 * 1000 // validade de 12 hrs
}
}));
servidor.use(passport.initialize());
servidor.use(passport.session());
servidor.use(flash());
require('./rotas')(servidor, passport);
servidor.listen(servidor.get('port'), function(){
console.log("Servidor rodando na porta: " + servidor.get('port'));
});
Authentication.js
var EstrategiaLocal = require('passport-local').Strategy,
Usuario = require('./bd/_usuario');
module.exports = function(passport){
passport.serializeUser(function(usuario, done) {
done(null, usuario.login);
});
passport.deserializeUser(function(login, done) {
Usuario.BuscarUm(login, function(erro, usuario){
if(erro){return done(erro)}
done(null, usuario)
});
});
passport.use('local', new EstrategiaLocal({
usernameField : 'login',
passwordField : 'senha',
passReqToCallback : true
},
function (req, nome_usuario, senha, done){
Usuario.BuscarUm(nome_usuario, function(usuario){
if(!usuario){
return done(null, false, req.flash('loginMessage', 'Usuario não encontrado.'));
}
if(usuario.VerificaSenha(senha)){
if(usuario.ativo){
return done(null, usuario);
}else{
return done(null, false, req.flash('loginMessage', 'Usuario não está ativo.'));
}
}else{
return done(null, false, req.flash('loginMessage', 'Senha ou usuário incorreto.'));
}
});
}
));
return passport;
};
routes
var get = require('./get'),
post = require('./post');
module.exports = function(app, passport){
post(app, passport);
get(app, passport);
}
get.js
module.exports = function(rota, passport){
rota.get('/login', function(req, res){
if(req.isAuthenticated()) res.redirect('adm/dashboard');
res.render('login');
});
rota.get('/adm/dashboard', EstaAutenticado, function(req, res){
res.render('adm/dashboard');
});
rota.get('/', EstaAutenticado, function(req, res){
res.render('adm/dashboard');
});
rota.get('/logout', EstaAutenticado, function(req, res){
req.logout();
req.session.destroy();
res.render('login');
});
};
function EstaAutenticado(req, res, next){
if(req.isAuthenticated())
return next();
res.redirect('/login');
};
post.js
module.exports = function(rota, passport){
rota.post('/login', passport.authenticate('local', {
successReturnToOrRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}));
};