passport-facebook-token
passport-facebook-token copied to clipboard
Catch InternalOAuthError: Failed to fetch user profile
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
I have the same error
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)
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})
}
}
)