注册新用户时,我没有重定向

时间:2018-08-18 15:04:20

标签: node.js passport.js

我使用node.js + password.js + sequelize.js(postgressql)

启动服务器并转到页面时,一切正常,并在我启动注册并向其发送请求时显示。然后,将数据库中的数据转移到数据库中的数据库中,从而在表中添加了新行,但是对重定向的请求结束了,因为它在几分钟内给出了该页面不可用的答案,我在做什么错了?

server.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const db = require('./db');
const morgan = require('morgan');
const flash = require('connect-flash');
require('./passport');

//require('./config/passport')(passport);
//const { Kik } = require('./db');
//const { proverkaUser } = require('./db');

app.use(bodyParser.urlencoded({extended: false}));
app.use(session({
  secret: 'oursercret',
  resave: true,
  saveUnitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(morgan('dev'));
app.use(require('./routes'));

app.set('view engine', 'ejs');
//app.set('views', './pages')




app.listen(8000, function(err){
  if(!err){
    console.log('OK');
  } else {
    console.log('WTF?');
  }
});

db.js

const Sequelize = require('sequelize');

var opt = {
  schema: 'kik_shema'
};

const db = new Sequelize('postgres://alejandro:Leomessi10@localhost:5434/bd', opt);

const User = db.define('kik', {
  email: Sequelize.STRING,
  password: Sequelize.STRING,
   
}, {
  timestamps: false, 
});

module.exports = {
  User,
  db
};

passport.js

const passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var { User } = require('./db');



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

  passport.deserializeUser(function(id, done){
    User.findById(id, function(err, user){
      done(err, user);
    });
  });

passport.use('signin', new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password'
    },
    async (email, password, done) => {
        try {
            const user = await User.findOne({
                where: { email }
            });

            if (!user) {
                done(null, false, {
                    message: 'Incorrect email'
                    // Better
                    // message: 'Incorrect email or password'
                });

                return;
            }

            if (user.password !== password) {
                done(null, false, {
                    message: 'Incorrect password'
                    // Better
                    // message: 'Incorrect email or password'
                });

                return;
            }

            done(null, user);
        } catch (error) {
            done(error);
        }
    })
);

passport.use('signup', new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password'
    },
    async (email, password, done) => {
        try {
            const findUser = await User.findOne({
                where: { email }
            });

            if (findUser) {
                done(null, false, {
                    message: 'Email already taken'
                });

                return;
            }

            const user = await User.create({
                email,
                password
            });

            done(null, user);
        } catch (error) {
            done(error);
        }
    }
));

routes.js

const passport = require('passport');
const { Router } = require('express');
const router = new Router();

router.get('/signup', (req, res) => {
    res.render('signup');
});

router.get('/signin', (req, res) => {
    res.render('signin');
});

router.get('/index', (req, res) => {
    res.render('index');
});

router.post('/index', (req, res) => {
    res.render('index');
});

router.post('/signup', passport.authenticate('signup', {
       successRedirect: '/index', //из passport.use берется signup
       failureRedirect: '/signup',
       failureFlash: true
     }));






module.exports = router;

1 个答案:

答案 0 :(得分:0)

上一则答案,并在注释中注明

Express'res.redirect()方法保留原始的HTTP方法类型。结果,当您的router.post('/signup', ...)处理程序重定向到/index/signup时,它将向这些地址发出POST请求。您没有针对这两个路由的router.post处理程序,因此您的服务器将忽略这些请求。

另请参阅: Node.js with Express: how to redirect a POST request