@zeytech/cognito-adonisjs
v2.0.4
Published
Zeytech AdonisJS plugin to help integrate Cognito for authentication
Downloads
115
Readme
Table of contents
cognito-adonisjs
cognito-adonisjs is a plugin created to make server setup of Cognito authorization quick and easy when using Adonis on your backend.
Local Development Testing
npm run publish:local
Then, in your consuming app:
npm uninstall @zeytech/cognito-adonisjs
npm i --save-peer ../path/to/cognito-adonisjs/pkg-local/zeytech-cognito-adonisjs-*.tgz
Example NPM script used in demo api (scripts block of package.json):
"update:plugin:cognito": "npm uninstall @zeytech/cognito-adonisjs && npm i --save-peer ../cognito-adonisjs/pkg-local/`ls ../cognito-adonisjs/pkg-local/*.tgz | xargs -n 1 basename`"
When testing, use the following process:
- In this repository/project:
npm run publish:local
- Stop dev server (if not already stopped)
- Restart dev server (
npm run dev:plugin
)
Installation
npm i @zeytech/cognito-adonisjs
Configuration
node ace configure @zeytech/cognito-adonisjs
- Question - Do you want me to generate a controller for Cognito service calls?
If yes, this will create a CognitoController.ts file in the app/Controller/Http folder with already created functions to access basic Cognito API calls.
- Automatic Action - Config file creation
A zeytech-cognito.ts file will be created in the config folder that contains config information for the plugin; do not edit this file.
- Question - Where would you like to show environment information?
In The Browser will show the information in your default browser and In Terminal will show the information in the terminal where you ran the node ace configure
command. Take the information shown and copy and paste it in the env.ts file.
Install Dependencies
- Redis
npm i redis
Add the following to the .env file (if not already there upon install):
REDIS_CONNECTION=local
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=
Ensure the following is added to the env.ts file (if not already there upon install):
REDIS_CONNECTION: Env.schema.enum(['local'] as const),
REDIS_HOST: Env.schema.string({ format: 'host' }),
REDIS_PORT: Env.schema.number(),
REDIS_PASSWORD: Env.schema.string.optional(),
env Variables
Add the following Cognito variables to the .env file:
COGNITO_CLIENT_ID=<client_id_from_AWS_Cognito_account>
COGNITO_USER_POOL_ID=<User_pool_ID_from_user_pool_that_was_created_in_AWS_Cognito_account>
COGNITO_DOMAIN=<Cognito_domain_found_in_AWS_Cognito_account_under_user_pool_App_Integration_tab>
COGNITO_REGION=<region_part_of_COGNITO_DOMAIN>
Ex: us-east-1
Usage
- In the start/kernel.ts file, add the following code under the Named Middleware section. If a Server.middleware.registerNamed block already exists, only add the 'cognitoAuth' line inside.
Server.middleware.registerNamed({
'cognitoAuth': () => import('@ioc:Adonis/Addons/Zeytech/AuthenticateMiddleware')
})
- In the start/routes.ts file, add .middleware('cognitoAuth') to any routes you would like to protect with Cognito authentication.
Ex:
Route.group(() => {
Route.group(() => {
Route.group(() => {
Route.get('/:id?', 'UserController.index')
Route.post('/', 'UserController.create')
}).prefix('/users')
}).middleware('cognitoAuth') <-- Added to Route group
}).prefix('/api')
Route.group(() => {
Route.group(() => {
Route.get('/:id?', 'UserController.index')
Route.post('/', 'UserController.create').middleware('cognitoAuth') <-- Added to individual route
}).prefix('/users')
}).prefix('/api')