# @servisbot/sb-auth

> A module for logging into Cognito for use with ServisBOT apps.

Latest version **2.2.0** (published 2026-11-05) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install @servisbot/sb-auth
pnpm add @servisbot/sb-auth
yarn add @servisbot/sb-auth
bun add @servisbot/sb-auth
```

## Health

**Score 55/100 (C)** — status: active.

Positive: no vulnerabilities; recently updated; high maintenance score.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2026-11-05 |
| First published | 2020-06-24 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ServisBOT |
| Maintainers | servisbotnpm |

## Links

- npm: https://www.npmjs.com/package/@servisbot/sb-auth
- Repository: https://github.com/servisbot/npm-sb-auth
- Homepage: https://github.com/servisbot/npm-sb-auth#readme
- Issues: https://github.com/servisbot/npm-sb-auth/issues
- npm.io page: https://npm.io/package/@servisbot/sb-auth

## Recent versions

- 2.2.0 (latest) — 2026-11-05
- 2.1.4 — 2025-02-18
- 2.1.3 — 2024-07-08
- 2.1.2 — 2024-04-09
- 2.1.1 — 2023-11-21
- 2.1.0 — 2023-11-17
- 2.0.5 — 2023-11-03
- 2.0.4 — 2023-11-02
- 2.0.3 — 2023-11-02
- 2.0.2 — 2023-11-02
- 2.0.1 — 2023-11-02
- 2.0.0 — 2023-11-01
- 1.2.1 — 2023-10-26
- 1.2.0 — 2023-09-06
- 1.0.4 — 2023-09-04
- … 12 more at https://npm.io/package/@servisbot/sb-auth/versions

## README

# npm-sb-auth

A module for logging into Cognito for use with ServisBOT apps.

## SBAuth

The instantiation of the SB Auth module requires the cookiejar url, and a fetch function.

```javascript
const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});
```

In all functions the returned object contains a key `result`, which if 'FAILURE' will include another key `message` detailing the nature of the failure.

### Login example

An example of logging a user in and getting back the jwt

```javascript
const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const organization = "flowit";
const username = "some@email.com";
const password = "myPassword";


const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "SUCCESS") {
  const jwt = await loginAttempt.user.getToken();
  //continue with jwt
}
```

User also has a getExpiresAt and a getUsername function

### Logging in with MFA enabled example

Similar to a regular login, instead of a success from the login you may get MFA_REQUIRED along with a session.
You can then call respondToMFAChallenge with the required token.

```javascript
let loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "MFA_REQUIRED") {
  // do whatever logic is needed to get an MFA token for this user
  // then call respondToMFAChallenge, using the session from the previous attempt

  loginAttempt = await sbAuth.respondToMFAChallenge(
    organization,
    username,
    loginAttempt.session,
    mfaToken
  );
  if (loginAttempt.result === "SUCCESS") {
    const jwt = await loginAttempt.user.getToken();
    //continue with jwt
  }
}
```

### Logging in and requiring a new password example

Similar to a regular login, instead of a success from the login you may get NEW_PASSWORD_REQUIRED along with a session.
You can then call respondToPasswordResetChallenge with a new password and the session.

```javascript
let loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "NEW_PASSWORD_REQUIRED") {
  //get the new password and pin from the user

  loginAttempt = await sbAuth.respondToPasswordResetChallenge(
    organization,
    username,
    newPassword,
    pin
  );
  if (loginAttempt.result === "SUCCESS") {
    const jwt = await loginAttempt.user.getToken();
    //continue with jwt
  }
}
```

### Login SSO example

Logging in a user with SSO is similar to the regular login, but requiring the SSO creds instead of username/password

```javascript
const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const organization = "flowit";
const code = "myCode";
const codeVerifier = "someCodeVerifier";
const redirectUri = "console.servisbot.com";

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

let loginAttempt = await sbAuth.loginSSO(
  organization,
  code,
  codeVerifier,
  redirectUri
);

if (loginAttempt.result === "SUCCESS") {
  const jwt = await loginAttempt.user.getToken();
  //continue with jwt
}
```

### Reset password example

It is possible to request a password reset for a user as follows

```javascript
const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const organization = "flowit";
const username = "myuser@email.com";

let resetAttempt = await sbAuth.requestPasswordReset(organization, username);

if (resetAttempt.result === "SUCCESS") {
  //the password was succesfully reset
}
```

### Logout example

```javascript
const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const organization = "flowit";
const username = "myuser@email.com";

await sbAuth.logout(organization);
```

### Respond To Complete New Password Challenge example

Similar to a regular login, instead of a success from the login you may get NEW_PASSWORD_REQUIRED along with a session.
You can then call respondToCompleteNewPasswordChallenge with the required token, to set a new password for the user

```javascript
let loginAttempt = await sbAuth.login(organization, username, password);

if (loginAttempt.result === "NEW_PASSWORD_REQUIRED") {
  // do whatever logic is needed to get an MFA token for this user
  // then call respondToMFAChallenge, using the session from the previous attempt

  loginAttempt = await sbAuth.respondToMFAChallenge(
    organization,
    username,
    loginAttempt.session,
    newPassword
  );

  if (resetAttempt.result === "SUCCESS") {
    //the password was succesfully reset
  }
}
```


### Refresh Token Example

Refreshes a token from the http cookie

```javascript
const fetch = require("node-fetch");
const { SBAuth } = require("@servisbot/sb-auth");

const SBAuthLib = SBAuth(fetch);

const sbAuth = new SBAuthLib({
  cookiejarUrl: "cookiejar.com",
});

const organization = "flowit";

const response = await sbAuth.refreshToken(organization);

// Successful response
const response = {
  result: 'SUCCESS',
  user: {
    "jwt": "some jwt",                // New JWT
    "jwtExpiresAt": 1720106966000,    // JWT expire epoch time
    "refreshTokenValidity": 120,      // JWT expire duration in minutes
    "username": "some@email.com",     // Username of the authenticated user
  }
}

// Network failure response
const response = {
  result: 'INTERNAL_SERVER_ERROR',
  message: 'Bad response from cookiejar'
}

// Invalid request response
const response = {
  result: 'INVALID_REQUEST_ERROR',
  message: 'Some error message'
}

```

---
_Source: https://npm.io/package/@servisbot/sb-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
