req.user.displayname 未定义 Nodejs + 护照 = google oauth
req.user.displayname undefined Nodejs + passport = google oauth
我试图在登录后访问 req.user.displayName,但我变得不确定。我错过了什么?
├── config
│ └── passport.js
├── controllers
│ ├── index.controllers.js
│ └── users.controllers.js
├── helpers
│ └── auth.js
├── models
├── public
├── routes
│ ├── index.routes.js
│ └── users.routes.js
├── server.js
└── views
config/passport.js ;
const { use } = require('passport');
const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const User = require('../models/user');
passport.serializeUser((user, done) => {
done(null, user.id);
})
passport.deserializeUser((user, done) => {
done(null, user);
})
passport.use(new GoogleStrategy({
clientID: 'blablablablablabla',
clientSecret: 'blablablabla',
callbackURL: "http://localhost:4000/google/callback",
passReqToCallback: true
},
async (req, token, refreshToken, profile, done) => {
const newUser = {
googleId: profile.id,
displayName: profile.displayName,
firstName: profile.name.givenName,
lastName: profile.name.familyName,
image: profile.photos[0].value,
}
try {
let user = await User.findOne({ googleId: profile.id })
if (user) {
done(null, user)
} else {
user = await User.create(newUser)
done(null, user)
}
} catch (err) {
console.error(err)
}
}
));
我正在尝试在登录后访问 req.user.displayName(重定向到“/”并传递 req.user.displayName 以在 ejs 视图中呈现)
controllers/index.controllers.js
const index_controller = {};
index_controller.renderHome = async (req, res) => {
res.render('home', { username: req.user.displyaName })
};
routes/index.routes.js
const { renderHome } = require('../controllers/index.controllers')
const { isAuth } = require('../helpers/auth')
router.get('/', isAuth, renderHome);
routes/users.routes.js
router.get('/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }), (req, res) => {
// res.send(req.user)
res.redirect('/');
});
你也可以在这里找到完整的代码:https://github.com/fcarreon/google-test
根据文档 (here),您应该使用:
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
您使用相同的代码对用户进行序列化和反序列化。
我试图在登录后访问 req.user.displayName,但我变得不确定。我错过了什么?
├── config
│ └── passport.js
├── controllers
│ ├── index.controllers.js
│ └── users.controllers.js
├── helpers
│ └── auth.js
├── models
├── public
├── routes
│ ├── index.routes.js
│ └── users.routes.js
├── server.js
└── views
config/passport.js ;
const { use } = require('passport');
const passport = require('passport')
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const User = require('../models/user');
passport.serializeUser((user, done) => {
done(null, user.id);
})
passport.deserializeUser((user, done) => {
done(null, user);
})
passport.use(new GoogleStrategy({
clientID: 'blablablablablabla',
clientSecret: 'blablablabla',
callbackURL: "http://localhost:4000/google/callback",
passReqToCallback: true
},
async (req, token, refreshToken, profile, done) => {
const newUser = {
googleId: profile.id,
displayName: profile.displayName,
firstName: profile.name.givenName,
lastName: profile.name.familyName,
image: profile.photos[0].value,
}
try {
let user = await User.findOne({ googleId: profile.id })
if (user) {
done(null, user)
} else {
user = await User.create(newUser)
done(null, user)
}
} catch (err) {
console.error(err)
}
}
));
我正在尝试在登录后访问 req.user.displayName(重定向到“/”并传递 req.user.displayName 以在 ejs 视图中呈现)
controllers/index.controllers.js
const index_controller = {};
index_controller.renderHome = async (req, res) => {
res.render('home', { username: req.user.displyaName })
};
routes/index.routes.js
const { renderHome } = require('../controllers/index.controllers')
const { isAuth } = require('../helpers/auth')
router.get('/', isAuth, renderHome);
routes/users.routes.js
router.get('/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }), (req, res) => {
// res.send(req.user)
res.redirect('/');
});
你也可以在这里找到完整的代码:https://github.com/fcarreon/google-test
根据文档 (here),您应该使用:
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
您使用相同的代码对用户进行序列化和反序列化。