Created
October 13, 2023 14:40
-
-
Save jenya239/e3424c1c0808741aea8caaa2063a65a9 to your computer and use it in GitHub Desktop.
passport oauth (facebook) state in verify
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
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