所以我已经使用Passport.js本地策略构建了一个注册和登录系统,但是我遇到了一些问题,我将在下面列出。
注意:我的服务器在PORT 5000上运行,而React在PORT 3000上运行并同时运行。
开机自检/注册302 60.982毫秒-0
注册失败
获取/注册200 8.697毫秒-22
但是,正如我所说,它仍在将数据发布并保存到数据库中。
2。登录系统遇到与上述相同的问题,因为我从控制台获得以下内容
POST /login 302 61.355 ms - 0
unsuccessful login
GET /login 200 8.974 ms - 20
route / apiRoutes.js
var mysql = require('mysql');
var bcrypt = require('bcryptjs');
var bodyParser = require('body-parser');
var urlencodedparser = bodyParser.urlencoded({ extended: false });
module.exports = function(app, passport) {
//MySQL Connection
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'sportsappdb'
});
connection.connect(function(err) {
if (!err) {
console.log('Database is connected');
} else {
console.log('Error while connecting with database');
}
});
app.use(bodyParser.json());
app.get('/signup', (req, res) => {
res.json('unsuccessful sign up');
console.log('unsuccessful sign up');
});
app.post(
'/signup',
passport.authenticate('local-signup', {
successRedirect: '/login',
failureRedirect: '/signup',
failureFlash: true
})
);
app.get('/login', function(req, res) {
res.json('unsuccessful login');
console.log('unsuccessful login');
});
app.post(
'/login',
passport.authenticate('local-login', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}),
function(req, res) {
res.json({ id: req.user.id, username: req.user.username });
if (req.body.remember) {
req.session.cookie.maxAge = 1000 * 60 * 3;
} else {
req.session.cookie.expires = false;
}
res.redirect('/auth');
}
);
app.get('/auth', passport.authenticate('local-login'), (req, res) => {
console.log('YEPP');
});
}; //End Module
Passport.js
var LocalStrategy = require('passport-local').Strategy;
var mysql = require('mysql');
var bcrypt = require('bcryptjs');
module.exports = function(passport) {
// ------------------MySQL Connection ---------------- need a way to not do this
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'sportsappdb'
});
connection.connect(function(err) {
if (!err) {
console.log('Database is connected');
} else {
console.log('Error while connecting with database');
}
});
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
connection.query('SELECT * FROM testtable WHERE id = ? ', [id], function(
err,
rows
) {
done(err, rows[0]);
});
});
passport.use(
'local-signup',
new LocalStrategy(
{
username: 'username',
password: 'password',
passReqToCallback: true
},
function(req, username, password, done) {
connection.query(
'SELECT * FROM testtable WHERE username = ?',
[username],
function(err, rows) {
if (err) return done(err);
if (rows.length) {
return done(
null,
false,
req.flash('signupMessage', 'That username is already taken.')
);
} else {
var newUserMysql = {
username: username,
password: password
// password: bcrypt.hashSync(password, null, null)
};
var insertQuery =
'INSERT INTO testtable( username, password ) values (?,?)';
connection.query(
insertQuery,
[newUserMysql.username, newUserMysql.password],
function(err, rows) {
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
}
);
}
}
);
}
)
);
passport.use(
'local-login',
new LocalStrategy(
{
username: 'username',
password: 'password',
passReqToCallback: true
},
function(req, username, password, done) {
connection.query(
'SELECT * FROM testtable WHERE username = ?',
[username],
function(err, rows) {
if (err) return done(err);
if (!rows.length) {
return done(
null,
false,
req.flash('loginMessage', 'bulunamadi.')
);
}
if (!bcrypt.compareSync(password, rows[0].password))
return done(
null,
false,
req.flash('loginMessage', 'yanlis parola.')
);
return done(null, rows[0]);
}
);
}
)
);
};