When calling the passport logout()
function to clear cookie session, http request gets stuck pending:
I set the routes in auth.routes.ts
:
router.get('/login', (req: Request, res: Response) => {
res.render("google-login");
});
router.post('/logout', (req, res, next) => {
req.logout(function(err) {
if (err) { return next(err); }
res.redirect('/auth/login');
});
});
and use the routes in app.ts
:
app.use(cookieSession({
maxAge: 24 * 60 * 60 * 1000, // 24h * 60m * 60s * 1000ms in a second
keys: [process.env.SESSION_KEY] // encrypt/decrypt using this key
}));
app.use(passport.initialize());
app.use(passport.session());''
app.use('/auth', auth_routes);
Users are being properly serialized/deserialized in passport-setup.ts
:
passport.serializeUser((user: any, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
logger.info("deserialize id: " + id);
GoogleUserModel.findById(id).then((user) => {
done(null, user);
});
})
Basic profile page to display user data profile.ejs
:
<body>
<nav>
<ul>
<form action="/auth/logout" method="POST">
<button type="submit">Logout</button>
</form>
<li><a href="/auth/login">Login</a></li>
<li><a href="/">Home</a></li>
</ul>
</nav>
<header>
<h2> Welcome <%= google_user.name %>! </h2>
<h4> Info </h4>
<p> Email: <%= google_user.email %> </p>
<p> Google ID: <%= google_user.google_id %> </p>
<p> Database ID: <%= google_user.id %> </p>
</header>
</body>
The cookie is still there as shown through the dev console:
When the logout
button is pressed the route is hit but gets stuck in the req.logout()
function. The correct user is being accessed before that as well. I'm just lost on why the request is getting stuck pending and not redirecting