passport-facebook-token icon indicating copy to clipboard operation
passport-facebook-token copied to clipboard

Catch InternalOAuthError: Failed to fetch user profile

Open antoinecsk opened this issue 8 years ago • 3 comments

Hi !

When my facebook token is expired, im receiving the 'InternalOAuthError: Failed to fetch user profile' (error 500).

Is there a way to send a 401 instead ? how can i catch this error ?

Regards

antoinecsk avatar Feb 11 '17 13:02 antoinecsk

I have the same error

ianomad avatar Feb 12 '17 23:02 ianomad

You can handle passport auth errors by wrapping the route and adding a callback:

router.post('/api/facebook/token', 
  (req, res, next) => passport.authenticate(
          'facebook-token',
          { failureRedirect: '/login' },
          (err, data) => {
            if(err){
              res.status(err.oauthError.statusCode)
              res.json(data || err)
            } else {
              res.json(data)
            }
          })(req, res, next)

micimize avatar Feb 18 '17 03:02 micimize

Agree with @micimize. Add a callback to handle the error. See @mkemalsan answer here: https://github.com/jaredhanson/passport-facebook/issues/93

My version:

router.get('/auth', passport.authenticate('facebook-token'),
    (req, res) => {
        if(req.user.err){
            res.status(401).json({
                success: false,
                message: 'Auth failed',
                error: req.user.err
            })
        }
        else if(req.user) {
            const user = {user_id: req.user.id}
            const token = jwt.sign(user, '##########', {
                expiresIn: "30d"
            })
            res.status(200).json({
                success: true,
                message: 'Enjoy your token!',
                token: token,
                user: req.user
            })
        } else {
            res.status(401).json({
                success: false,
                message: 'Auth failed'
            })
        }
    },
    // add this callback for handling errors. Then you can set responses with codes or 
    // redirects as you like.
    (error, req, res, next) => {
        if(error) {
            res.status(400).json({success: false, message: 'Auth failed', error})
        }
    }
)

akapei avatar Oct 18 '17 01:10 akapei