gpt4 book ai didi

javascript - TypeError : req. session.regenerate 不是使用 Passport 的函数

转载 作者:行者123 更新时间:2023-12-05 00:28:30 27 4
gpt4 key购买 nike

您好,我正在学习 Node with React full web stack 类(class),但我遇到了一个大问题,我不知道它为什么会出现,或者从什么出现。
我的终端显示此错误:

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 时出现了

最佳答案

我在做同样的类(class),在udemy上有修复。来自 Udemy:
由于不兼容,Passport v.0.6.0 目前已损坏:
https://github.com/jaredhanson/passport/issues/907
维护者建议使用最新的 v0.5.0,直到推出修复程序。利用:
npm卸载 Passport
npm 安装 Passport @0.5
这对我来说很好。

关于javascript - TypeError : req. session.regenerate 不是使用 Passport 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72375564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com