Skip to content

Instantly share code, notes, and snippets.

@jenya239
Created October 13, 2023 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenya239/e3424c1c0808741aea8caaa2063a65a9 to your computer and use it in GitHub Desktop.
Save jenya239/e3424c1c0808741aea8caaa2063a65a9 to your computer and use it in GitHub Desktop.
passport oauth (facebook) state in verify
this.strategy = new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
passReqToCallback: true,
profileFields: ['id', 'emails', 'name'],
callbackURL: '/oauth2/redirect/facebook',
store: true
}, (req, accessToken, refreshToken, profile, cb) => {
d(`\n\noauth state in verify = `, req.oauthState)
return cb(null, {})
})
passport.use(this.strategy)
const hardcodedOauthState = {
type: 'registration',
currencyId: 'f565acb1-688d-4d5e-a70f-3de88bdf96f5'
}
app.get('/login2/facebook', passport.authenticate('facebook', {state: hardcodedOauthState}))
app.get('/oauth2/redirect/facebook', (req, res, next) => {
// we can't access it inside verify because of
// https://github.com/jaredhanson/passport-oauth2/blob/ea9e99adda82dff67502654347589866fea80eb2/lib/state/store.js#L77
// `delete req.session[key];`
const stateObj = req.session[this.strategy._stateStore._key].state
const stateHandle = req.query.state
if (stateObj.handle !== stateHandle) {
console.error('smth wrong')
}
req.oauthState = stateObj.state // hardcodedOauthState
console.log('finally oauth state', req.oauthState)
next()
},
passport.authenticate('facebook', {
session: false
}), (req, res) => {
// yes we have req.authInfo.state here but it called after verify
res.send('ok')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment