Created
September 7, 2023 09:37
-
-
Save tilleps/185c81baa4b943f81d101a8b2a6b547b to your computer and use it in GitHub Desktop.
PassportJS promise support workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Add Promise support for PassportJS: req.logIn() and req.logOut() | |
// | |
import { promisify } from "node:util"; | |
import passportHttpRequest from "passport/lib/http/request.js"; | |
// Workaround: Convert to promises before passport.initialize() | |
app.use(function (req, res, next) { | |
req.logIn = promisify(passportHttpRequest.logIn); | |
req.logOut = promisify(passportHttpRequest.logOut); | |
next(); | |
}); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.get("/login", async function (req, res, next) { | |
try { | |
const user = { id: "1234" }; | |
await req.login(user, { keepSessionInfo: true }); | |
res.send("LOGGED IN"); | |
} catch (err) { | |
return next(err); | |
} | |
}); | |
app.get("/logout", async function (req, res, next) { | |
try { | |
await req.logout({ keepSessionInfo: true }); | |
res.send("LOGGED OUT"); | |
} catch (err) { | |
return next(err); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment