This is a sample code to show case how to retrieve tokens from a Cognito Hosted UI and use them to invoke an API Gateway secured by Cognito.
- Setup Cognito Hosted UI as described in the documentation
- Fill-in the
./src/assets/pool-config.js
file with the information from the User Pool you just created:
const pool = {
USER_POOLID: '',
CLIENTID: '',
HOSTED_UI: 'https://<domain>.<region>.amazoncognito.com',
REGION: 'us-east-1',
...
}
- Setup the project with the instruction shown below (Project setup)
- You need to serve the project from an https route as Cognito only accepts HTTPS redirect URL with a FQDN (ie it will not accept https://localhost/ or https://127.0.0.1).
- Add the https hosting URI followed by
/done
as theHTTPS_REDIRECT_GW_URI
in thepool-config.js
file
const pool = {
...
HTTPS_REDIRECT_GW_URI: ''
}
-
Create a REST API in API Gateway using Cognito authentication
-
Customize the
callApi
method in.src/views/Code.vue
callApi: function() {
var api_gateway_url = 'https://xxxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/demo/';
axios.get(api_gateway_url, {
headers: { 'Authorization' : this.idToken }
}).then((data) => console.log(data)).catch((err) => console.log(err))
}
npm install
npm run serve
npm run build
npm run lint