Cod*_*lby 33 javascript mongodb node.js express passport.js
你好,我正在学习 Node with React full web stack 课程,我遇到了一个大问题,我不知道它为什么会出现,也不知道它出现的内容。我的终端显示此错误:
req.session.regenerate(function(err) {
^ TypeError: req.session.regenerate is not a function
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样
索引.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);
Run Code Online (Sandbox Code Playgroud)
护照.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));
}
});
} ) );
Run Code Online (Sandbox Code Playgroud)
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);
});
};
Run Code Online (Sandbox Code Playgroud)
我真的不知道问题是从哪里来的,我可以说当我要去的时候这个问题就会出现localhost:5000/auth/google
小智 55
我正在做同样的课程,udemy 上有修复。来自 Udemy :
Passport v.0.6.0 目前由于不兼容而被破坏: https://github.com/jaredhanson/passport/issues/907
维护者建议使用最新的 v0.5.0,直到推出修复程序。使用:
npm 卸载护照
npm 安装 Passport@0.5
这对我来说效果很好。
小智 9
我发现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()
})Run Code Online (Sandbox Code Playgroud)
https://github.com/jaredhanson/passport/issues/907
npm uninstall passport
npm install passport@0.5
Run Code Online (Sandbox Code Playgroud)
我认为发生此错误是因为护照req.session.regenerate() 在内部使用函数(源代码链接),cookie-session没有regenerate方法,因此护照抛出错误。
GitHub 页面上还有一个cookie-session与功能相关的问题regenerate(问题链接)。
如果在客户端保留会话并不重要,您可以将其保留在服务器端,您可以为此使用express-session ,那么您将不会收到上述错误。