Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Catch InternalOAuthError: Failed to fetch user profile #67

Open
antoinecsk opened this issue Feb 11, 2017 · 3 comments
Open

Catch InternalOAuthError: Failed to fetch user profile #67

antoinecsk opened this issue Feb 11, 2017 · 3 comments

Comments

@antoinecsk
Copy link

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

@ianomad
Copy link

ianomad commented Feb 12, 2017

I have the same error

@micimize
Copy link

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)

@akapei
Copy link

akapei commented Oct 18, 2017

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})
        }
    }
)

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

No branches or pull requests

4 participants