-
Notifications
You must be signed in to change notification settings - Fork 80
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
"InternalOAuthError: Failed to fetch user profile" thrown when access_token is invalid #62
Comments
Didn't test it but this could work. Just to give you an idea. This can be optimised. app.post(
"/login",
(req, res) => {
passport.authenticate('facebook-token', function (err, user, info) {
console.error(err);
})(req, res);
}
); |
Hi, I was struggling with the exact same issue, @Schnueggel's advice helped me to solve it: app.post(
"/login",
(req, res) => {
passport.authenticate('facebook-token', function (err, user, info) {
if(err){
if(err.oauthError){
var oauthError = JSON.parse(err.oauthError.data);
res.send(oauthError.error.message);
} else {
res.send(err);
}
} else {
res.send(user);
}
})(req, res);
} With this I can access meaningful information in err.oauthError.data/error.message which I can send to the client:
Cheers, |
thank you for sharing that @throbi anyone know why i'm getting the |
Happening to me as well, I'm using this strategy with loopback. |
Add a callback to handle the error. See @mkemalsan answer here: 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})
}
}
) |
I am using passport-facebook-token to authenticate users who use a RESTful API. When I use a valid access_token, everything works fine.
However, if I delete or replace a character in my access_token and then submit an API request, then I get the following error:
InternalOAuthError: Failed to fetch user profile
This causes the server to crash. Instead of this, I want to be able to choose what error message I send back to the client, and avoid crashing the server whenever someone sends me an invalid access_token.
Here is the code to reproduce the error:
The text was updated successfully, but these errors were encountered: