I have tried to search for solutions for this for a while, but I'm still quite new to programming. I found this article about a similar issue, but none of the comments or solutions have worked for me, ref:
https://stackoverflow.com/questions/45541182/passport-req-logout-function-not-working
and also this one: Why is PassportJS in Node not removing session on logout
Here is my HTML and JS code:
<!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!");
});
Thanks in advance!
CodePudding user response:
You should configure the passport before the logout function calls.
app.use(passport.initialize());
app.use(passport.session());
The logout should be called after the above code.
Refer - https://codedec.com/tutorials/logout-using-passport-module-in-node-js/