我正在尝试使用localstrategy实现passport.js但无法重定向到成功路由。每当我点击提交按钮时,它总是将我重定向到失败的路线。为什么会这样,我该怎么办?
表格代码:
<form action="/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/>
</div>
<div>
<label>Password:</label>
<input type="password" name="password"/>
</div>
<div>
<input type="submit" value="Log In"/>
</div>
</form>
app.js文件代码:
var express = require('express');
var app = express();
var connection = require('express-myconnection');
var mysql = require('mysql');
var customers = require('./routes/routes');
var bodyParser = require('body-parser');
var passport = require('passport')
var flash = require('connect-flash')
var LocalStrategy = require('passport-local').Strategy;
var urlencodedParser = bodyParser.urlencoded({
extended: false
})
app.set('view engine', 'ejs');
app.use(
connection(mysql, {
host: 'localhost',
user: 'root',
password: '',
database: 'nodejs'
}, 'request')
);
app.get('/', customers.form);
passport.use('local', new LocalStrategy(
function(username, done) {
console.log('abc');
customers.checkStatus(username, function (err, user) {
if (err) throw err;
if (!user) {
return done(null, false, {message: 'Unknown User'});
}
});
}));
app.post('/login',
passport.authenticate('local', { successRedirect: '/success',
failureRedirect: '/' }),
function (req,res) {
console.log('tets');
res.redirect('/');
}
);
var server = app.listen(8080, function() {
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});
route.js文件代码:
exports.form = function(req,res){
res.render('form', {
page_title: "Login Form"
});
};
exports.success = function (req, res) {
res.send('wow');
};
exports.checkStatus = function (req, res, username) {
req.getConnection(function(err, connection) {
connection.query('select * from login where username = ?',
[username], function(err, rows) {
});
});
}
答案 0 :(得分:0)
因为我能够解决你的问题,不能重定向到失败,登录后它进入护照功能并打印放在控制台中的任何内容。但是在登录表单中输入详细信息后,它将重定向到数据库连接。在那里,我得到 TypeError:req.getConnection不是一个函数我试图解决它。即使我无法解决这个问题。我没有太多想法连接mysql到nodejs.I我正在研究mongodb.Here我正在尝试解决此问题的链接req.getConnection is not a function in express-myconnection
用这个替换app.js代码我做了一些更改
var express = require('express');
var path = require('path');
var ejs = require('ejs');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require('express-session');
var customers = require('./routes/routes');
var bodyParser = require('body-parser');
var passport = require('passport')
var flash = require('connect-flash')
var LocalStrategy = require('passport-local').Strategy;
var urlencodedParser = bodyParser.urlencoded({
extended: false
})
var app = express();
var mysql = require('mysql'),
connection = require('express-myconnection');
app.use(
connection(mysql, {
host: 'localhost',
user: 'root',
password: '',
database: 'nodejs'
}, 'request')
);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({secret: '{secret}', name: 'session_id', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.set('view engine', 'ejs');
passport.use('local', new LocalStrategy(
function(username, done) {
console.log('abc');
customers.checkStatus(username, function (err, user) {
if (err) throw err;
else{
if (!user) {
return done(null, false, {message: 'Unknown User'});
}
if(user){
res.send("User");
}
}
});
}));
app.get('/login', customers.form);
app.get('/success',customers.success);
app.get('/fail',customers.fail)
app.post('/login', passport.authenticate('local', { successRedirect: '/success',
failureRedirect: '/fail',
failureFlash: true
}));
var server = app.listen(8080, function() {
var port = server.address().port;
console.log("Example app listening at http://localhost:%s", port);
});
这是routes.js代码
exports.checkStatus = function (req, res, next) {
req.getConnection(function(err, connection) {
if (err) return next(err);
connection.query('SELECT * FROM login', function(err, rows) {
if (err) console.log("Error Selecting : %s ", err);
res.render('form', {
page_title: "Login form", data: rows
});
});
});
};
exports.form = function(req,res){
res.render('/give the path of form.ejs', { message: req.flash('loginMessage')
});
}
exports.success = function (req, res) {
res.send('wow');
};
exports.fail= function (req, res) {
res.send('Noooooo');
};
打开浏览器并尝试使用此http://localhost:8080/login
。希望这对您有所帮助......