您好,我正在遵循课程节点与React全Web堆栈,我有一个大问题,我不知道为什么它会出现,或从什么是显示.我的终端显示此错误:
req.session.regenerate(function(err) {
^ TypeError: req.session.regenerate is not a function
我的代码看起来像是
index.js
const express = require("express");
const mongoose = require("mongoose");
const cookieSession = require("cookie-session");
const passport = require("passport");
const keys = require("./config/keys");
require("./models/User");
require("./services/passport");
mongoose.connect(keys.connect_url);
const app = express();
app.use( cookieSession({
maxAge: 30 * 24 * 60 * 60 * 1000,
keys: [keys.cookieKey], }) );
app.use(passport.initialize());
app.use(passport.session());
require("./routes/authRoutes")(app);
const PORT = process.env.PORT || 5000;
app.listen(PORT);
passport.js
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const mongoose = require("mongoose");
const keys = require("../config/keys");
const User = mongoose.model("users");
passport.serializeUser((user, done) => { done(null, user.id); });
passport.deserializeUser((id, done) => {
User.findById(id).then((user) => {
done(null, user); }); });
passport.use(new GoogleStrategy(
{
clientID: keys.googleClientId,
clientSecret: keys.googleClientSecret,
callbackURL: "/auth/google/callback",
},
(accessToken, refreshToken, profile, done) => {
User.findOne({ googleId: profile.id }).then((existingUser) => {
if (existingUser) {
// we already have a record with the given profile ID
done(null, existingUser);
} else {
// we don't have a user record with this ID, make a new record!
new User({ googleId: profile.id })
.save()
.then((user) => done(null, user));
}
});
} ) );
authRouters.js
const passport = require("passport");
module.exports = (app) => {
app.get(
"/auth/google",
passport.authenticate("google", {
scope: ["profile", "email"],
})
);
app.get("/auth/google/callback", passport.authenticate("google"));
app.get("/api/current_user", (req, res) => {
res.send(req.user);
});
};
我真的不知道这个问题是从哪里来的,我可以说,当我进入localhost:5000/auth/google
时,这个问题就出现了
4条答案
按热度按时间5us2dqdw1#
我正在做同样的课程,修复是有对udemy.从Udemy:
Passport v.0.6.0当前由于不兼容而中断:https://github.com/jaredhanson/passport/issues/907
维护人员建议使用最新的v0.5.0版本,直到推出修复程序用途:
npm卸载passport
npm安装密码@0.5
这对我很有效。
mqkwyuun2#
我认为发生此错误是因为passport在内部使用
req.session.regenerate()
函数(source code link),cookie-session
没有regenerate
方法,因此passport抛出此错误。cookie-session
GitHub页面上也有一个与regenerate
函数(issue link)相关的问题。如果将会话保留在客户端并不重要,您可以将其保留在服务器端,您可以使用express-session来实现这一点,那么您将不会得到上述错误。
xkftehaa3#
我遇到这个问题的时间也不长......它似乎是一个问题的cookie会话包......为了解决这个问题,我使用了express-session
lnvxswe24#
https://github.com/jaredhanson/passport/issues/907