req.logout() 不是函数

req.logout() is not a function

一段时间以来,我一直在尝试寻找解决方案,但我对编程还是很陌生。我找到了这篇关于类似问题的文章,但是 none 的评论或解决方案对我有用,参考:

以及这个:Why is PassportJS in Node not removing session on logout

这是我的 HTML 和 JS 代码:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <title>Success!</title>
        <link rel="stylesheet" href="css/styles.css">
        <!-- Google Fonts -->
        <link rel="preconnect" href="https://fonts.googleapis.com">
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@100;300;400;500;700;900&display=swap" rel="stylesheet">
    </head>
    <body>
        <h1>Success!</h1>
            <form action="/success" method="post">
                <button type="submit"></button>
            </form>
    </body>
</html>

---------------------------------------------------------------------------------------------

JS CODE HERE:

---------------------------------------------------------------------------------------------

require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.set("view engine", "ejs");

const session = require("express-session");



app.use(session({
    resave:false,
    saveUninitialized: true,
    secret: (hush)
}));

app.use(express.static("public"));

app.use(bodyParser.urlencoded({
    extended: true
}));


app.get("/", (req, res) => {
    res.redirect("/login");
});

app.get("/login", (req, res) => {
    res.sendFile(__dirname + "/public/login.html")
})

app.get("/success", function(req, res) {
    res.sendFile(__dirname + "/public/success.html")
})


app.get('/logout', (req, res) => {
    req.logout();
    req.session.destroy(()=>{
      res.redirect('/');
    });
  });
app.post("/success", function(req, res) {
    console.log("Deleting");
    
    res.redirect("/logout");
})


const passport = require("passport");
const res = require("express/lib/response");
var userProfile;


app.use(passport.initialize());
app.use(passport.session());

app.get("/success", function(req, res) {
if (req.user) {
    res.send(userProfile);
} else {
    res.redirect("/login")
}});

app.get("/error", (req, res) => res.send("error logging in"));

passport.serializeUser(function(user, cb) {
    cb(null, user)
});

passport.deserializeUser(function(obj, cb) {
    cb(null, obj)
});
const GoogleStrategy = require("passport-google-oauth").OAuth2Strategy;
const GOOGLE_CLIENT_ID = process.env.CLIENT_ID;
const GOOGLE_CLIENT_SECRET = process.env.CLIENT_SECRET;

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "https://tverrfaglig.herokuapp.com/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
    userProfile=profile;
    return done (null, userProfile);
}
));

app.get("/auth/google",
    passport.authenticate("google", { scope : ["profile", "email"] }));

app.get('/auth/google/callback', 
    passport.authenticate('google', {
        successRedirect: '/success',
        failureRedirect: '/login'
    })
);


app.listen(process.env.PORT || "3000", function() {
    console.log("Server started!");
});




提前致谢!

您应该在注销函数调用之前配置通行证。

app.use(passport.initialize());
app.use(passport.session());

注销应该在上面的代码之后调用。

参考 - https://codedec.com/tutorials/logout-using-passport-module-in-node-js/