Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

New applications state feature: req.authInfo isn't available in error handler #140

Open
plashenkov opened this issue Jul 5, 2021 · 1 comment

Comments

@plashenkov
Copy link

plashenkov commented Jul 5, 2021

Hi! I'm trying to read the application state in an error handler. It works fine inside a success handler, but inside the error handler req.authInfo is empty. But we may need the state there too to fully handle failure situations on a par with success situation.

Example code:

// Let's suppose our frontend app generates a token and passes it to OAuth flow.
// After either success or failure the app wants to get it back and compare with
// the initial value to check the final callback is legitimate and initiated by the app itself.

app.get(
  '/api/#/facebook',

  function (req, res, next) {
    const options = {
      state: {
        token: req.query.token
      }
    }
    passport.authenticate('facebook', options)(req, res, next)
  }
)

app.get(
  '/api/#/facebook/callback',

  passport.authenticate('facebook', {failWithError: true})

  function (req, res) {
    const params = {
      result: 'success',
      token: req.authInfo.state.token,
    }
    res.redirect('/#?' + (new URLSearchParams(params)).toString())
  },

  function (err, req, res, next) {
    const params = {
      result: 'error',
      message: err.message,
      token: req.authInfo.state.token, // <-- the problem
    }
    res.redirect('/#?' + (new URLSearchParams(params)).toString())
  }
)
@plashenkov
Copy link
Author

plashenkov commented Jul 5, 2021

Just checked another way to handle success and failure situations: passsport.authenticate()'s callback. We don't have req.authInfo there either.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant