如何添加login身份validation和会话到ReactJS React路由器和Redux与MongoDB + NodeJS Express?

我有一个MongoDB + NodeJS Express服务器,运行Webpack并能够向数据库注册一个用户凭证。 我现在想在用户login时添加身份validation和会话。我正在查看Passport.js但似乎无法掌握如何将所有可以放在一起。

最好的办法是什么? 任何示例和指导将不胜感激。

到目前为止,这些都是我设立的。

对于服务器:

 var express = require('express'); var path = require('path'); var config = require('../webpack.config.js'); var webpack = require('webpack'); var webpackDevMiddleware = require('webpack-dev-middleware'); var webpackHotMiddleware = require('webpack-hot-middleware'); var bodyParser = require('body-parser'); var MongoClient = require('mongodb').MongoClient; var ObjectId = require('mongodb').ObjectID; var app = express(); var db; var compiler = webpack(config); app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: config.output.publicPath})); app.use(webpackHotMiddleware(compiler)); app.use(express.static('dist')); app.use(bodyParser.json()); app.post('/api/users/', function(req, res) { console.log('Req body', req.body); var newUser = req.body; db.collection('users').insertOne(newUser, function(err, result) { if(err) console.log(err); var newId = result.insertedId; db.collection('users').find({_id: newId}).next(function(err, doc) { if(err) console.log(err); res.json(doc); }) }) }) app.get('/', function (req, res) { res.sendFile(path.resolve('client/index.html')); }); MongoClient.connect('mongodb://localhost/testdb', function(err, dbConnection) { if(err) console.log(err); db = dbConnection; var server = app.listen(3000, function() { var port = server.address().port; console.log("Started server at port", port); }); }); 

而对于路由器(login页面在Login和注册页面正在呈现在Register ):

 //react-router render( <div> <Provider store={store}> <Router history={hashHistory}> <Route component ={MainPage} path='/' > <IndexRoute component={Login}/> <Route component={Register} path="Register" /> </Route> </Router> </Provider> </div>, document.getElementById('app') ) 

Reduce中的API POST请求:

 //How registered user's credentials are stored via action in redux registerUser(username, email, name, password) { console.log('Started registering request') var user = { username: username, email: email, name: name, password: password, } var request = { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(user) } console.log('Adding user: ', user) fetch(`http://localhost:3000/api/users`, request) .then(res => res.json()) .then(user => { console.log(user); return user; }) .catch(err => { console.log('Error is', err) }) console.log('Ending registering request') }, 

       

网上收集的解决方案 "如何添加login身份validation和会话到ReactJS React路由器和Redux与MongoDB + NodeJS Express?"