Passport 不会 return 除了用户名和密码以外的字段
Passport wouldn't return fields other than username and password
在我的设置中,路由被编程为在成功验证后打印 Passport 返回的所有数据:
// routes/auth-providers/local.js
import express from 'express';
import passport from 'passport';
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import passportSetup from '../../passport-setup';
dotenv.config();
const router = express.Router();
router.post('/',
passport.authenticate('local', { failureRedirect: '/' }),
(req, res) => {
const {username, firstName, lastName} = req.body;
console.log('req.body', req.body);
res.json({ success: true });
}
);
我的本地策略配置如下:
// passport-setup/strategies/local.js
const local = new LocalStrategy (
(username, password, done) => {
User.findOne({ username: username }, (err, user) => {
if(err) { return done(err); }
if (!user) { return done(null, false); }
// if (!user.verifyPassword(password)) { return done(null, false); }
console.log('USER', user);
return done(null, user);
});
}
);
在两个 console.log()
语句中,只有第二个给出了整个用户文档,包括 firstName、lastName 等。但是,我的路线中的第一个 (console.log('req.body', req.body);
),只提供用户名和密码。我什至可以看到正在序列化的用户文档:
// passport-setup/index.js
passport.serializeUser((user, done) => {
console.log('SERIALIZE', user);
done(null, user.id);
});
我可能做错了什么?
P.S.: 回购在 https://github.com/amitschandillia/proost/tree/master/web 供参考。
我还是 passport.js
的新手,但在文档 here 中提到:
If authentication succeeds, the next handler will be invoked and the req.user
property will be set to the authenticated user
所以也许您需要做的只是将 req.body
更改为 req.user
:
const {username, firstName, lastName} = req.user;
在我的设置中,路由被编程为在成功验证后打印 Passport 返回的所有数据:
// routes/auth-providers/local.js
import express from 'express';
import passport from 'passport';
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import passportSetup from '../../passport-setup';
dotenv.config();
const router = express.Router();
router.post('/',
passport.authenticate('local', { failureRedirect: '/' }),
(req, res) => {
const {username, firstName, lastName} = req.body;
console.log('req.body', req.body);
res.json({ success: true });
}
);
我的本地策略配置如下:
// passport-setup/strategies/local.js
const local = new LocalStrategy (
(username, password, done) => {
User.findOne({ username: username }, (err, user) => {
if(err) { return done(err); }
if (!user) { return done(null, false); }
// if (!user.verifyPassword(password)) { return done(null, false); }
console.log('USER', user);
return done(null, user);
});
}
);
在两个 console.log()
语句中,只有第二个给出了整个用户文档,包括 firstName、lastName 等。但是,我的路线中的第一个 (console.log('req.body', req.body);
),只提供用户名和密码。我什至可以看到正在序列化的用户文档:
// passport-setup/index.js
passport.serializeUser((user, done) => {
console.log('SERIALIZE', user);
done(null, user.id);
});
我可能做错了什么?
P.S.: 回购在 https://github.com/amitschandillia/proost/tree/master/web 供参考。
我还是 passport.js
的新手,但在文档 here 中提到:
If authentication succeeds, the next handler will be invoked and the
req.user
property will be set to the authenticated user
所以也许您需要做的只是将 req.body
更改为 req.user
:
const {username, firstName, lastName} = req.user;