0.5.2 • Published 3 years ago

@restlessness/auth-cognito v0.5.2

Weekly downloads
50
License
MIT
Repository
github
Last release
3 years ago

@restlessness/auth-cognito npm version

Installation

From you restlessness project root run:

$ # Install node package
$ npm i @restlessness/auth-cognito
$
$ # Add it to the project using the restlessness cli
$ restlessness add-auth @restlessness/auth-cognito

The add-auth command adds the package to the project and creates an AppUserPoolsManager model class.

Environment variables

NameDescription
RLN_COGNITO_AUTH_USER_POOL_IDuser pool id
RLN_COGNITO_AUTH_USER_CLIENT_IDuser client id
RLN_COGNITO_AUTH_USER_REGIONuser region
RLN_COGNITO_AUTH_USER_REDIRECT_URIredirect uri
RLN_COGNITO_AUTH_ACCESS_KEY_IDaccess key id
RLN_COGNITO_AUTH_SECRET_ACCESS_KEYsecret access key

The add-auth command also adds parametric environment variables to the .env files present in the project.

Example .env.production file:

RLN_COGNITO_AUTH_USER_POOL_ID=${RLN_COGNITO_AUTH_USER_POOL_ID_PRODUCTION}

Then is possible to set the variable by setting RLN_COGNITO_AUTH_USER_POOL_ID_PRODUCTION or setting RLN_COGNITO_AUTH_USER_POOL_ID directly.

Usage

The add-auth command create a model class implementing the UserPoolsManager class. With this model can be specified the list of pools.

Example:

// src/models/AppUserPoolsManager/index.ts

import { UserPoolsManager } from '@restlessness/auth-cognito';

class AppUserPoolsManager extends UserPoolsManager {
  get poolInfos() {
    return [
      {
        id: 'user',
        attributes: [],
      },
      {
        id: 'my-id',
        attributes: [ 'my' , 'list', 'of', 'attributes' ],
      },
    ];
  }
};

export default new AppUserPoolsManager();

All convenience methods of the UserPoolsManager can be accessed from the extended class:

import userPoolsManager from '../models/AppUserPoolsManager';
// ...
console.log(userPoolsManager.poolInfos)
const userSession = await userPoolsManager.login('sample@email.com', 'sample_password');
// ...

Hooks

The package uses the beforeEndpoint hook to initialize the user pool manager (AppUserPoolsManager).

Documentation

UserPoolsManager class

Fields:
  • pools: UserPoolManager[]\ A list of user pool manager
Methods:
  • abstract get poolInfos(): PoolInfo[]\ Implement this method to specify the user pools\ Returns: PoolInfo - the list of pools to be managed

  • init(): Promise<void>\ Initialize all the pool managers (field pools) specified by the poolInfos method\ Returns: the MongoDao object used to perform all the query

  • getUserPoolById(id: string): UserPoolManager\ id: the id of the manager to be returned\ Returns: the user pool manager identified by the specified id

UserPoolManager object

Methods:
  • signup(email: string, password: string, values: any): Promise<CognitoSignUpResult>\ Returns: signup result

  • login(email: string, password: string): Promise<CognitoUserSession>\ Returns: user session

  • getOAuth2TokensFromCode(code: string): Promise<CognitoTokens>\ Returns: CognitoTokens - the tokens object

  • recoveryPassword(email: string): Promise<any>

  • resetPassword(email: string, verificationCode: string, newPassword: string): Promise<any>

  • confirmAccount(username: string, code: ConfirmationCodeType): Promise<void>

  • refreshTokens(idToken: string, refreshToken: string): Promise<CognitoUserSession>

  • adminUpdateAttributes(email: string, attributes: object): Promise<void>

  • adminCreateUser(email: string, password: string, attributes?: AttributeListType): Promise<any>

  • verifyMFA(cognitoUserSession: CognitoUserSession, username: string, verificationCode: string): Promise<CognitoUserSession>\ Returns: user session

  • changePassword(email: string, oldPassword: string, newPassword: string): Promise<void>

  • adminChangeUserPassword(email: string, newPassword: string): Promise<void>

  • adminGetUser(username: UsernameType)

Types

PoolInfo

interface PoolInfo {
  id: string
  attributes: string[]
}

CognitoTokens

interface CognitoTokens {
  idToken: string
  accessToken: string
  refreshToken: string
}
0.5.2

3 years ago

0.5.0

4 years ago

0.5.1

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.6

4 years ago

0.3.7

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.5

4 years ago

0.2.3

4 years ago

0.2.1

4 years ago

0.0.0

4 years ago