每次我尝试登录时都会返回
Cannot set property 'user' of undefined
,我尝试更新.env中的秘密并将护照更新到最新版本。我已按照文档来满足参数。在日志中我可以看到它收集用户信息,但回调失败。
我也尝试按照here的建议将护照降级到0.2.2,但没有结果。
App.js
require('dotenv').config();
const express = require("express");
// const bodyParser = require("body-parser");
const _ = require("lodash");
const app = express();
const https = require("https");
const mongoose = require('mongoose');
const session = require('express-session');
const passport = require('passport'),
GoogleStrategy = require('passport-google-oauth20').Strategy,
TwitterStrategy = require('passport-twitter').Strategy,
FacebookStrategy = require('passport-facebook').Strategy;
const MemoryStore = require('memorystore')(session)
const passportLocalMongoose = require('passport-local-mongoose');
const findOrCreate = require('mongoose-findorcreate');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true}));
// ----------- Mongo
const url = process.env.MONGODB_URI;
const connectionParams = {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
}
mongoose.connect(url,connectionParams)
.then( () => {
console.log('Connected to database ')
})
.catch( (err) => {
console.error(`Error connecting to the database. \n${err}`);
})
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: 'auto',
maxAge: 86400000
},
store: new MemoryStore({
checkPeriod: 86400000
})
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.set("useCreateIndex", true);
const userSchema = new mongoose.Schema({
googleId: String,
profileImage: String,
myCollection: {
type: Object,
default: Object
},
games: {
type: Object,
default: Object
},
homebrews: {
type: Object,
default: Object
}
});
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
const User = new mongoose.model("User", userSchema);
// ----------- Google
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "https://necronomicon.app/auth/google/callback",
userProfileUrl: "https://www.googleapis.com/oauth2/v3/userinfo",
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
console.log(profile);
User.findOrCreate({
googleId: profile.id,
username: profile.displayName,
profileImage: profile.photos[0].value
},
function(err, user) {
return done(err, user);
});
}
));
app.get('/auth/google', passport.authenticate('google', {
scope: ["profile"],
prompt: "select_account"
}));
passport.use(User.createStrategy());
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
app.get('/auth/google/callback', passport.authenticate('google', {
failureRedirect: "/"
}),
function(req, res) {
console.log("Successful authentication, redirect home.");
res.redirect("/");
});
package.json 依赖项
"bcrypt": "^5.0.1",
"blob": "^0.1.0",
"body-parser": "^1.19.0",
"canvas-toBlob": "^1.0.0",
"connect-ensure-login": "^0.1.1",
"cookie": "^0.4.0",
"cookie-parser": "^1.4.4",
"dotenv": "^8.2.0",
"ejs": "^3.0.1",
"express": "^4.17.1",
"express-favicon": "^2.0.1",
"express-session": "^1.17.1",
"file-saver": "^2.0.5",
"html-to-image": "^1.7.0",
"html2canvas": "^1.4.0",
"jquery": "^3.5.1",
"js-cookie": "^2.2.1",
"lodash": "^4.17.20",
"memorystore": "^1.6.7",
"mongoose": "^5.10.0",
"mongoose-encryption": "^2.0.2",
"mongoose-findorcreate": "^3.0.0",
"node-boolify": "^1.0.6",
"passport": "^0.6.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth2": "^0.2.0",
"passport-google-oauth20": "^2.0.0",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^6.0.1",
"passport-twitter": "^0.1.5",
"xmldom": "^0.1.5"
错误
TypeError: Cannot set property 'user' of undefined
Aug 26 04:23:07 PM at /opt/render/project/src/node_modules/passport-oauth/node_modules/passport/lib/passport/http/request.js:45:35
Aug 26 04:23:07 PM at pass (/opt/render/project/src/node_modules/passport/lib/authenticator.js:274:43)
Aug 26 04:23:07 PM at serialized (/opt/render/project/src/node_modules/passport/lib/authenticator.js:283:7)
Aug 26 04:23:07 PM at /opt/render/project/src/app.js:174:3
Aug 26 04:23:07 PM at pass (/opt/render/project/src/node_modules/passport/lib/authenticator.js:291:9)
Aug 26 04:23:07 PM at Authenticator.serializeUser (/opt/render/project/src/node_modules/passport/lib/authenticator.js:296:5)
Aug 26 04:23:07 PM at IncomingMessage.req.login.req.logIn (/opt/render/project/src/node_modules/passport-oauth/node_modules/passport/lib/passport/http/request.js:43:29)
Aug 26 04:23:07 PM at Strategy.strategy.success (/opt/render/project/src/node_modules/passport/lib/middleware/authenticate.js:256:13)
Aug 26 04:23:07 PM at verified (/opt/render/project/src/node_modules/passport-oauth2/lib/strategy.js:189:20)
Aug 26 04:23:07 PM at /opt/render/project/src/app.js:166:16
Aug 26 04:23:07 PM at /opt/render/project/src/node_modules/mongoose-findorcreate/index.js:66:11
Aug 26 04:23:07 PM at /opt/render/project/src/node_modules/mongoose/lib/model.js:5074:18
Aug 26 04:23:07 PM at processTicksAndRejections (internal/process/task_queues.js:77:11)
我也有同样的错误。奇怪的是,现在我用 git clone 重新创建了项目,它给了我错误。在另一台具有相同项目和库的电脑上,没有问题。
你解决了吗?