nestjs-sage-id
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

Nestjs-sage-id

Injectable client to authenticate to Sage Api's

Table Of Contents

About

nestjs-sage-id provides a client that exposes a user-friendly interface that communicates with the sage-id API in order to manage the sage tokens used for authentication and authorization to Sage API's.

Installation

npm install --save nestjs-sage-id

Getting Started

The simplest way to use nestjs-sage-id is to use SageIdModule.register. However, since the credentials used to authenticate through the sage-id are confidential, it is encouraged to use environment variable(s) or an external storage method. Either option will most likely be done in an asynchronous context, so the example will use the SageIdModule.registerAsync function.

import { Module } from '@nestjs/commosetSageTokenn';
import { ConfigService, ConfigModule } from '@nestjs/config';
import { SageIdModule } from 'nestjs-sage-id';

@Module({
  imports: [
    SageIdModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        authRedirectUri: configService.get<string>(
            'authRedirectUri',
        )
        clientId: configService.get<string>('clientId'),
        clientSecret: configService.get<string>('secret'),
        companyId: configService.get<number>('companyId'),
        retryStrategy: {
          maxRetryAttempts: 3,
          scalingDuration: 300,
          excludedStatusCodes: [],
        },
        subscriptionPrimaryKey: configService.get<string>(
          'subscriptionPrimaryKey',
        ),
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [SageService],
  exports: [SageService],
})
export class SageModule {}

This will provide an injectable instance of the SageIdService to the module providers.

import { forwardRef, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { SageAccessToken, SageIdService } from 'nestjs-sage-id';

@Injectable()
export class SageService {
  constructor(private readonly sageIdService: SageIdService) {}

  async getCachedSageRefreshToken(): Promise<string> {
    // retrieve the refresh token of the stored token
  }

  async setSageToken(authorizationCode: string): Promise<void> {
    const token = await this.sageIdService.getSageToken(authorizationCode);
    // store the token
  }

  async refreshSageToken(): Promise<void> {
    const token = await this.sageIdService.refreshSageToken(
      await this.getCachedSageRefreshToken(),
    );
    // store the token
  }
}

Issues

Please include an MRE (Minimal reproducible example) when providing issues.

Contributing

All contributions are appreciated. The more, the merrier. Please make sure to follow the below guidelines when contributing.

  1. Fork the repository
  2. Create your branch (git checkout -b my-branch)
  3. Commit any changes to your branch
  4. Push your changes to your remote branch
  5. Open a pull request

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgments

Copyright © 2022 Jonas Grønbek

Readme

Keywords

Package Sidebar

Install

npm i nestjs-sage-id

Weekly Downloads

0

Version

0.0.5

License

MIT

Unpacked Size

117 kB

Total Files

46

Last publish

Collaborators

  • jonasgroenbek