-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from buccfer/docs-improvements
Docs improvements
- Loading branch information
Showing
10 changed files
with
151 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,10 @@ jobs: | |
local_dir: out | ||
target_branch: gh-pages | ||
keep_history: false | ||
committer_from_gh: true | ||
committer_from_gh: true | ||
- stage: Deploy to NPM | ||
if: branch = master | ||
deploy: | ||
provider: npm | ||
email: [email protected] | ||
api_key: $NPM_AUTH_TOKEN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
Most use cases require that the user data is fetched from a database. For example, when you want to check the user permissions | ||
or retrieve some data based on the user settings. In that case, you can create your own authentication middleware reusing the | ||
one provided by this library. | ||
|
||
In the following example we will use the [compose-middleware](https://www.npmjs.com/package/compose-middleware) package to create | ||
our own authentication middleware that will: | ||
|
||
1. Validate the JWT. | ||
2. If the JWT is valid, it will fetch the user from the *User* MongoDB collection. | ||
|
||
```javascript | ||
// authentication.middleware.js | ||
'use strict'; | ||
|
||
const HttpErrors = require('http-errors'); | ||
const mongoose = require('mongoose'); | ||
const { compose } = require('compose-middleware'); | ||
const { authenticate } = require('aws-cognito-express'); | ||
|
||
// Custom middleware to fetch the user from the 'User' MongoDB collection. | ||
async function fetchUser(req, res, next) { | ||
const { email } = req.cognito; | ||
const User = mongoose.model('User'); | ||
|
||
try { | ||
const user = await User.findOne({ email }); | ||
|
||
if (!user) { | ||
// NOTE: You should create an error handler for http errors. | ||
return next(new HttpErrors.Unauthorized(`User with email ${email} does not exist`)); | ||
} | ||
|
||
req.user = user; | ||
return next(); | ||
} catch (err) { | ||
return next(err); | ||
} | ||
} | ||
|
||
module.exports = compose([ | ||
authenticate({ | ||
region: 'us-east-2', | ||
userPoolId: 'us-east-2_6IfDT7ZUq', | ||
tokenUse: ['id', 'access'], | ||
audience: ['55plsi2cl0o267lfusmgaf67pf'] | ||
}), | ||
fetchUser | ||
]); | ||
``` | ||
|
||
Finally, you can use your custom middleware as follows: | ||
|
||
```javascript | ||
// app.js | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const { authenticationError } = require('aws-cognito-express'); | ||
const customAuthenticationMiddleware = require('./authentication.middleware'); | ||
|
||
const app = express(); | ||
|
||
// Add the custom authentication middleware. | ||
app.use(customAuthenticationMiddleware); | ||
|
||
// Protected route. | ||
app.get('/articles', (req, res, next) => { | ||
console.log('Logged in user: ', req.user); | ||
}); | ||
|
||
// Add the authentication error handler. | ||
app.use(authenticationError()); | ||
|
||
module.exports = app; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
This library uses the [debug](https://www.npmjs.com/package/debug) package for debugging, which is disabled by default. To enable it, | ||
you have to set the *DEBUG* environment variable as follows: | ||
|
||
```text | ||
DEBUG=aws-cognito-express | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
{ | ||
"01-quick-start": { | ||
"title": "Quick Start" | ||
"title": "Quick start" | ||
}, | ||
"02-custom-validation": { | ||
"title": "Custom Validation & Error Handling" | ||
"02-fetch-user-from-database": { | ||
"title": "Fetching users from a database" | ||
}, | ||
"03-testing": { | ||
"03-custom-authentication": { | ||
"title": "Custom authentication flow" | ||
}, | ||
"04-testing": { | ||
"title": "Testing" | ||
}, | ||
"05-debugging": { | ||
"title": "Debugging" | ||
} | ||
} |