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

Add documentation for failWithError #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions docs/authenticate.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ app.post('/login',

By default, if authentication fails, Passport will respond with a
`401 Unauthorized` status, and any additional route handlers will not be
invoked. If authentication succeeds, the next handler will be invoked and the
`req.user` property will be set to the authenticated user.
invoked. Use the [Fail with error](#fail-with-error) option to pass failures
through the next error handling middleware. If authentication succeeds, the
next handler will be invoked and the `req.user` property will be set to the authenticated user.

Note: Strategies must be configured prior to using them in a route. Continue
reading the chapter on [configuration](/guide/configure/) for details.
Expand Down Expand Up @@ -96,6 +97,44 @@ app.get('/api/users/me',
});
```

## Fail with Error

Return failures through your own error handling middleware.

```javascript
app.post('/login',
passport.authenticate('local', { failWithError: true },
function(req, res) {
// If this function gets called, authentication was successful.
// `req.user` contains the authenticated user.
res.redirect('/users/' + req.user.username);
});
```

Setting the `failWithError` option to `true` instructs Passport to throw an error
that can be caught by route or app middleware.

```javascript
app.use(err, req, res, next) {
// This catches errors
res.status(401).json({success: false, err: err, msg: "Error caught"});
});
```

The above will return the following JSON.

```javascript
{
"success": false,
"err": {
"name": "AuthenticationError",
"message": "Unauthorized",
"status": 401
},
"msg": "Error Caught"
}
```

## Custom Callback

If the built-in options are not sufficient for handling an authentication
Expand Down