Skip to content
225 changes: 152 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,99 @@
<p align="left">
<h1 align="center">
<a href="https://scalekit.com" target="_blank" rel="noopener noreferrer">
<picture>
<img src="https://cdn.scalekit.cloud/v1/scalekit-logo-dark.svg" height="64">
<picture >
<img src="https://cdn.scalekit.cloud/v1/scalekit-logo-dark.svg" height="50" style="padding: 1rem;">
</picture>
</a>
<br/>
</p>

# Official Node.js SDK
<a href="https://scalekit.com" target="_blank" rel="noopener noreferrer">Scalekit</a> is an Enterprise Authentication Platform purpose built for B2B applications. This Node.js SDK helps implement Enterprise Capabilities like Single Sign-on via SAML or OIDC in your Node.js applications within a few hours.
<a href="https://www.npmjs.com/package/@scalekit-sdk/node">
<img src="https://img.shields.io/npm/v/@scalekit-sdk/node.svg?color=3742B3&style=for-the-badge&labelColor=0C0D16">
</a>
<a href="https://www.scalekit.com/?intent=earlyaccess&utm_source=docs">
<img src="https://img.shields.io/badge/Get_Early_Access-4f5eff?style=for-the-badge&labelColor=E7E7E8&color=3742B3&labelColor=0C0D16">
</a>
<a>
<img alt="NPM Downloads" src="https://img.shields.io/npm/d18m/%40scalekit-sdk%2Fnode?style=for-the-badge&color=3742B3&labelColor=0C0D16">
</a>
<a href="https://docs.scalekit.com">
<img src="https://img.shields.io/badge/%F0%9F%92%A1%20-docs-a173ff.svg?style=for-the-badge&labelColor=0C0D16&color=3742B3">
</a>
<a>
<img alt="GitHub License" src="https://img.shields.io/github/license/scalekit-inc/scalekit-sdk-node?style=for-the-badge&color=3742B3&labelColor=0C0D16">
</a>

<div>
📚 <a target="_blank" href="https://docs.scalekit.com">Documentation</a> - 🚀 <a target="_blank" href="https://docs.scalekit.com">Quick-start Guide</a> - 💻 <a target="_blank" href="https://docs.scalekit.com/apis">API Reference</a>
</div>
<hr />
</h1>

## Pre-requisites
<p align="center">
<em><a href="https://scalekit.com">Scalekit</a> is your gateway to <b>enterprise-readiness</b> for B2B and SaaS applications. Built on <a href="https://openid.net/">OpenID</a> and <a href="https://en.wikipedia.org/wiki/SAML_2.0">SAML</a> standards, it provides seamless <b>authentication</b>, <b>user management</b>, and <b>access control</b> capabilities. Scalekit enables you to focus on <b>core product development</b> while we handle the complexities of <b>enterprise authentication</b>.</em>
</p>

1. [Sign up](https://scalekit.com) for a Scalekit account.
2. Get your ```env_url```, ```client_id``` and ```client_secret``` from the Scalekit dashboard.
---

## Installation
## ⚙️ Installation

Install Scalekit SDK using your preferred package manager.
Scalekit's Node.js SDK requires **Node.js version 18.x** or later to run. To install or upgrade Node.js, refer to the [official Node.js installation guide](https://nodejs.org/en/download). To start setting up, go to your Node project's root directory and run following command:

```sh
npm install @scalekit-sdk/node
#or
```

Other package managers like Yarn and pnpm are also supported.

```sh
yarn add @scalekit-sdk/node
#or
```

```sh
pnpm add @scalekit-sdk/node
```

```sh
bun add @scalekit-sdk/node
```

## Usage

Initialize the Scalekit client using the appropriate credentials. Refer code sample below.
Scalekit SDK establishes an enterprise-ready authentication server that can be set up for an application by providing API credentials (Currently, Early Access) during SDK initialization.

```javascript
import { ScalekitClient } from "@scalekit-sdk/node";
```js
import { ScalekitClient } from '@scalekit-sdk/node';

const scalekitClient = new ScalekitClient(
process.env.SCALEKIT_ENV_URL!,
process.env.SCALEKIT_CLIENT_ID!,
process.env.SCALEKIT_CLIENT_SECRET!
const scalekit = new ScalekitClient(
process.env.SCALEKIT_ENV_URL,
process.env.SCALEKIT_CLIENT_ID,
process.env.SCALEKIT_CLIENT_SECRET
);

// Use the sc object to interact with the Scalekit API
const authUrl = scalekitClient.getAuthorizationUrl("https://acme-corp.com/redirect-uri", {
state: "state",
connectionId: "connection_id",
});

```

## Examples - SSO with Express.js

Below is a simple code sample that showcases how to implement Single Sign-on using Scalekit SDK
<details>
<summary> 💭 Remind me the sequence of single sign-on</summary>
<figure>
<img src="./images/1.png" style="text-align: center;">
<figcaption>Sequence where Scalekit fits in your application</figcaption>
</figure>
</details>
<br/>

```javascript
import express from "express";
import { ScalekitClient } from "@scalekit-sdk/node";

const app = express();

const sc = new ScalekitClient(
process.env.SCALEKIT_ENV_URL!,
process.env.SCALEKIT_CLIENT_ID!,
process.env.SCALEKIT_CLIENT_SECRET!
);
/**
* Import ScalekitClient as scalekit
* Create an Express app instance
*/

const redirectUri = `${process.env.HOST}/auth/callback`;

// Get the authorization URL and redirect the user to the IdP login page

app.get("/auth/login", (req, res) => {
const authUrl = scalekitClient.getAuthorizationUrl(
redirectUri,
const authUrl = scalekit.getAuthorizationUrl(
redirectUri,
{
state: "state",
connectionId: "connection_id",
}
);

res.redirect(authUrl);
});

// Handle the callback from Scalekit
app.get("/auth/callback", async (req, res) => {
const { code, error, error_description, idp_initiated_login } = req.query;
// Handle error
Expand All @@ -94,14 +103,15 @@ app.get("/auth/callback", async (req, res) => {
// Handle IdP initiated login
if (idp_initiated_login) {
// Get the claims from the IdP initiated login
const {
connection_id,
organization_id,
login_hint,
relay_state
} = await scalekitClient.getIdpInitiatedLoginClaims(idp_initiated_login as string);
// Get the authorization URL and redirect the user to the IdP login page
const url = scalekitClient.getAuthorizationUrl(
const {
connection_id,
organization_id,
login_hint,
relay_state
} = await scalekit.getIdpInitiatedLoginClaims(idp_initiated_login as string);

// Get the authorization URL and redirect the user to the IdP login page
const url = scalekit.getAuthorizationUrl(
redirectUri,
{
connectionId: connection_id,
Expand All @@ -118,28 +128,97 @@ app.get("/auth/callback", async (req, res) => {
return res.json(authResp.accessToken);
});

app.listen(3000, () => {
console.log("Server is running on port 3000");
});
```

## Example Apps
## 🧩 API

Fully functional sample applications written using some popular web application frameworks and Scalekit SDK. Feel free to clone the repo and run them locally.
#### scalekit.getAuthorizationUrl(redirectUri, options)

- [Express.js](https://github.com/scalekit-inc/scalekit-express-example.git)
- [Next.js](https://github.com/scalekit-inc/scalekit-nextjs-example.git)
Return Type: `string`

## API Reference
Returns the authorization URL and redirect the user to the IdP login page

Refer to our [API reference docs](https://docs.scalekit.com/apis) for detailed information about all our API endpoints and their usage.
```sh
https://auth.scalekit.com/authorize
?client_id=skc_1220XXXXX349527
&redirect_uri=https://yourapp.com/auth/callback
&provider=google
```

## More Information
**`redirectUri`**

- Quickstart Guide to implement Single Sign-on in your application: [SSO Quickstart Guide](https://docs.scalekit.com)
- Understand Single Sign-on basics: [SSO Basics](https://docs.scalekit.com/best-practices/single-sign-on)
Type: `string`

## License
The URL to redirect the user to after the user has logged in. For example, `https://b2b-app.com/auth/callback`.

This project is licensed under the **MIT license**.
See the [LICENSE](LICENSE) file for more information.
**`options`**

Type: `object`

Any one of the following identifiers can be used to generate the Authorization URL:

- `connectionId` - The connection ID to use for the login. For example, `conn_1220XXXXX349527`.
- `organizationId` - The organization ID to use for the login. For example, `org_1220XXXXX349527`.
- `loginHint` - The login hint to use for the login. For example, `[email protected]`.

Read more about [Authorization URL](https://docs.scalekit.com/sso/guides/key-concepts/authorization-url)

#### scalekit.authenticateWithCode(code, redirectUri, options?)

Return Type: `object`

Returns an object containing user profile details (`idToken` in JSON Web Token format) along with access token.

**`code`**

Type: `string`

The code sent to the redirect URL (`/auth/callback`) in it's redirect query parameter.

**`redirectUri`**

Type: `string`

The URL to redirect the user to after the user has logged in. For example, `https://b2b-app.com/auth/callback`.

#### scalekit.getIdpInitiatedLoginClaims(idpInitiatedLoginToken)

Return Type: `object`

Returns the identifiers needed to generate the authorization URL when users login using their Identity Provider (IdP). (a.k.a. IdP initiated login)

**`idpInitiatedLoginToken`**

Type: `string` (JSON Web Token format)

The value of `idp_initiated_login` query parameter from the redirect URL.

For detailed information on each option and parameter, please refer to our [API Reference](https://docs.scalekit.com/apis) 📚.

## 🔍 Examples

Scan through the examples for [Express.js](https://github.dev/scalekit-inc/scalekit-express-example) and [Next.js](https://github.dev/scalekit-inc/scalekit-nextjs-example).

```sh
# Clone Express.js example
git clone https://github.com/scalekit-inc/scalekit-express-example.git

# Clone Next.js example
git clone https://github.com/scalekit-inc/scalekit-nextjs-example.git
```

## 🤝 Contributing

Thank you for considering contributing to the Scalekit SDK!

- Feel free to open an issue or submit a pull request.
- Become a [stargazer](https://github.com/scalekit-inc/scalekit-sdk-node/stargazers) of this repository.
- Write a blog post of what you built with Scalekit.

---

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

<p align="center">
Made with ❤️ by the Scalekit team
</p>
Binary file added images/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.