1.5.1 • Published 2 months ago

@zeytech/cognito-adonisjs v1.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Table of contents

cognito-adonisjs

npm-image license-image typescript-image

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')
1.5.1

2 months ago

1.5.0

5 months ago

1.2.18

7 months ago

1.3.6

7 months ago

1.3.5

7 months ago

1.3.4

7 months ago

1.3.3

7 months ago

1.3.2

7 months ago

1.4.0

6 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.3.10

7 months ago

1.3.13

7 months ago

1.3.11

7 months ago

1.3.12

7 months ago

1.3.17

7 months ago

1.3.15

7 months ago

1.3.16

7 months ago

1.3.9

7 months ago

1.3.8

7 months ago

1.2.12

7 months ago

1.2.13

7 months ago

1.2.10

7 months ago

1.2.11

7 months ago

1.2.16

7 months ago

1.2.17

7 months ago

1.2.14

7 months ago

1.2.15

7 months ago

1.2.9

1 year ago

1.2.7

1 year ago