TypeError:req.session.regeneres不是使用护照的功能
您好,我正在关注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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我正在做同样的课程,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.
我发现“ noreferrer”> philippkrauss的帖子他建议在使用Passport 0.6的同时实施工作。 0。
I found philippkrauss's post of his suggested implementation to work while using passport 0.6.0.
https://github.com/jaredhanson/jaredhanson/passport/issues/907
https://github.com/jaredhanson/passport/issues/907
我认为发生此错误是因为护照使用
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 notregenerate
method and therefor passport throws the error.There is also an issue on the
cookie-session
GitHub page that is related toregenerate
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.
我面临的问题也不是很长……似乎是cookie-session套餐的问题...为了解决这个问题,我使用了明确的课程
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
使用Express JWT,您只能将Coockie存储为HTTP,我现在可以使用它。
With express jwt you can store the coockie as http only, I have it working now.
为我解决的问题是从饼干会议转换为明确的课程。
What solved it for me was to switch from cookie-session to express-session.