TypeError:req.session.regeneres不是使用护照的功能

发布于 2025-02-01 01:42:05 字数 2675 浏览 2 评论 0原文

您好,我正在关注React Full 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

Hello I'm following the course Node with React full web stack, and I got a big issue, I don't know why it's showing up, or from what is showing up.
my terminal shows this error :

req.session.regenerate(function(err) {
               ^ TypeError: req.session.regenerate is not a function

and my code looks like it

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);
      });
    };

I really don't know from it issue comes in, I can say that is showing up when I'm going to localhost:5000/auth/google

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

另类 2025-02-08 01:42:06

我正在做同样的课程,udemy上有修复。来自Udemy:

Passport V.0.6.0目前由于不兼容而被损坏:
https://github.com/jaredhanson/jaredhanson/passport/issues/907

最新的v0.5.0直到将修复程序推出为止。使用:

NPM卸载护照

NPM安装

Am doing the same course, fix is there on udemy. From Udemy :

Passport v.0.6.0 is currently broken due to an incompatibility:
https://github.com/jaredhanson/passport/issues/907

The maintainer suggests using the latest v0.5.0 until a fix is pushed out. Use:

npm uninstall passport

npm install [email protected]

this worked fine for me.

原野 2025-02-08 01:42:06

我发现“ noreferrer”> philippkrauss的帖子他建议在使用Passport 0.6的同时实施工作。 0。

app.use(cookieSession({
    // ...
}))
// register regenerate & save after the cookieSession middleware initialization
app.use(function(request, response, next) {
    if (request.session && !request.session.regenerate) {
        request.session.regenerate = (cb) => {
            cb()
        }
    }
    if (request.session && !request.session.save) {
        request.session.save = (cb) => {
            cb()
        }
    }
    next()
})

I found philippkrauss's post of his suggested implementation to work while using passport 0.6.0.

app.use(cookieSession({
    // ...
}))
// register regenerate & save after the cookieSession middleware initialization
app.use(function(request, response, next) {
    if (request.session && !request.session.regenerate) {
        request.session.regenerate = (cb) => {
            cb()
        }
    }
    if (request.session && !request.session.save) {
        request.session.save = (cb) => {
            cb()
        }
    }
    next()
})

七禾 2025-02-08 01:42:06

我认为发生此错误是因为护照使用req.Session.Regenerate()函数内部功能(

cookie-session github页面上还有一个问题,该页面与recenerate函数有关(问题链接)。

如果将会话保留在客户端上并不重要,则可以将其保留在服务器端,可以使用 Express-Session 为此,您将无法获得上述错误。

I think this error happens because the passport use req.session.regenerate() function internally (source code link), cookie-session has not regenerate method and therefor passport throws the error.

There is also an issue on the cookie-session GitHub page that is related to regenerate function (issue link).

If it is not important to keep the session on the client-side, you can keep it on the server-side, you can use express-session for this, then you will not get the above error.

花海 2025-02-08 01:42:06

我面临的问题也不是很长……似乎是cookie-session套餐的问题...为了解决这个问题,我使用了明确的课程

// install the package
npm install express-session

const session = require('express-session')

app.use(session({
   secret: 'somethingsecretgoeshere',
   resave: false,
   saveUninitialized: true,
   cookie: { secure: true }
}));

I faced the problem not quite long also... it seems to be a problem with the cookie-session package... To resolve this I used the express-session

// install the package
npm install express-session

const session = require('express-session')

app.use(session({
   secret: 'somethingsecretgoeshere',
   resave: false,
   saveUninitialized: true,
   cookie: { secure: true }
}));
污味仙女 2025-02-08 01:42:06
app.use(cookieSession({
    // ...
}))
// register regenerate & save after the cookieSession middleware initialization
app.use(function(request, response, next) {
  if (request.session && !request.session.regenerate) {
    request.session.regenerate = (cb) => {
      cb()
    }
  }
  if (request.session && !request.session.save) {
    request.session.save = (cb) => {
      cb()
    }
  }
  next()
})
app.use(cookieSession({
    // ...
}))
// register regenerate & save after the cookieSession middleware initialization
app.use(function(request, response, next) {
  if (request.session && !request.session.regenerate) {
    request.session.regenerate = (cb) => {
      cb()
    }
  }
  if (request.session && !request.session.save) {
    request.session.save = (cb) => {
      cb()
    }
  }
  next()
})
︶葆Ⅱㄣ 2025-02-08 01:42:06

使用Express JWT,您只能将Coockie存储为HTTP,我现在可以使用它。

With express jwt you can store the coockie as http only, I have it working now.

零崎曲识 2025-02-08 01:42:06

为我解决的问题是从饼干会议转换为明确的课程。

What solved it for me was to switch from cookie-session to express-session.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文