在登录系统(Sequelize,Passport,Express,Postgres)中获取模板时出现500错误

时间:2018-11-05 02:31:46

标签: postgresql express sequelize.js passport.js http-status-code-500

我在进行下一步时遇到问题。如果我在登录访问中输入了正确的值(签入数据库),则不会进入下一步,但是,如果我输入了错误的值,它将进入正确的索引。

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var expressValidator = require('express-validator')
var session = require('express-session');
var passport = require('passport');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users_logic');

var app = express();


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(require('connect-flash')());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(cookieParser());
app.use(session({secret: 'keykams', saveUninitialized: false, resave: false}));
//app.use(express.static(path.join(__dirname, 'public')));

require('./config/passport')(passport);

app.use(passport.initialize());
app.use(passport.session());

app.use('/static', express.static( 'static'));

app.use('/', indexRouter);
app.use('/users', usersRouter);

module.exports = app;

passport.js

var LocalStrategy = require('passport-local').Strategy;
const {profesores, alumnos} = require('../models/articles');
const config = require('../config/database');



module.exports = function(passport){

    passport.use('local', new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true
      }, function(req, email, password, done){
          profesores.findOne({where: {email: email, password:password}}).then(function(user) {//function(err, user){
            //console.log(user);
            if (!user) {
                return done(null, false);
            }
            /*if (!user.validPassword(password)) {
                return done(null, false);
            }*/
            return done(null, user);
          });
      })
      );


      passport.serializeUser(function(user, done) {
        done(null, user.id)

      });
      try{
         passport.deserializeUser(function(id, done) {
        profesores.findByPk(id).then(function(user){
          done(user);
        });
      });
    }catch (err) {
        console.log(err);
    }
}

index.js

var express = require('express');
var router = express.Router();
var path = require('path');
var bodyParser = require('body-parser');
var db = require('../config/database');
var {profesores, alumnos} = require("../models/articles");
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;


router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());


router.get('/', function(req, res, next) {
  res.render('index');
});

router.get('/inicio_alumnos', function(req, res, next) {
  res.render('inicio_alumnos')
});


router.post('/dologin_profesores', 
  passport.authenticate('local', { failureRedirect: '/', successRedirect:'/inicio_alumnos' }),
  function(req, res) {
    res.redirect('/inicio_alumnos');
  });
  

这是错误消息:

Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."email" = 'antonio@antonio' AND "profesores"."password" = 'antonio' LIMIT 1;
POST /dologin_profesores 302 119.856 ms - 74
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
GET /inicio_alumnos 500 526.758 ms - 2161
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
GET /css/materialize.min.css 500 55.015 ms - 2161
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
GET /static/error.css 500 106.534 ms - 2161
GET /static/loader.webp 500 166.429 ms - 2161
GET /js/materialize.min.js 500 197.454 ms - 2161
GET /static/error-404.png 500 217.978 ms - 2161
Executing (default): SELECT "id", "username", "password", "email", "pdni", "pdniinput", "edad", "grado", "createdAt", "updatedAt" FROM "terax"."profesores" AS "profesores" WHERE "profesores"."id" = 4;
GET /favicon.ico 500 44.630 ms - 2161

如您所见,正在获取数据,匹配并且没有下一步。

0 个答案:

没有答案