npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mahdaad/auth-nodejs-sdk

v1.0.4

Published

TypeScript/JavaScript SDK for Auth Service with authentication, user management, and service role operations

Readme

@mahdaad/auth-nodejs-sdk

TypeScript/JavaScript SDK for Auth Service with authentication, user management, and service role operations.

npm version Node.js Version

Installation

npm install @mahdaad/auth-nodejs-sdk

Requirements

  • Node.js >= 18.0.0
  • Redis (optional, for caching)

Quick Start

import AuthSDK from "@mahdaad/auth-nodejs-sdk";

// Initialize SDK
const sdk = new AuthSDK("http://localhost:3000");

// Login
const result = await sdk.auth.login("[email protected]", "password123");
console.log("Access Token:", result.accessToken);

// Get current user
const user = await sdk.user.getUser();
console.log("User:", user);

Features

  • TypeScript Support - Full type definitions included
  • ✅ User authentication (register, login, logout)
  • ✅ OTP authentication (email/SMS)
  • ✅ Token management (refresh, revoke)
  • ✅ User profile management
  • ✅ Redis caching support (30 seconds TTL for user data)
  • ✅ Service role operations (admin functions)
  • ✅ Native Node.js fetch (no external dependencies except Redis)

TypeScript Support

This package is written in TypeScript and includes full type definitions:

import AuthSDK, { type User, type AuthResponse } from "@auth-service/sdk";

const sdk = new AuthSDK("http://localhost:3000");
const user: User = await sdk.user.getUser();

Usage

Basic Authentication

import AuthSDK from "@mahdaad/auth-nodejs-sdk";

const sdk = new AuthSDK("http://localhost:3000");

// Register
const registerResult = await sdk.auth.register(
  "[email protected]",
  "Password123"
);

// Login
const loginResult = await sdk.auth.login("[email protected]", "Password123");
sdk.setAccessToken(loginResult.accessToken);

// Get current user
const user = await sdk.user.getUser();

// Logout
await sdk.auth.logout(loginResult.accessToken, loginResult.refreshToken);

With Redis Caching

import AuthSDK from "@mahdaad/auth-nodejs-sdk";

// Initialize with Redis
const sdk = new AuthSDK("http://localhost:3000", "redis://localhost:6379");

// Login
const loginResult = await sdk.auth.login("[email protected]", "Password123");
sdk.setAccessToken(loginResult.accessToken);

// First call - fetches from API
const user1 = await sdk.user.getUser();

// Second call within 30 seconds - uses Redis cache
const user2 = await sdk.user.getUser(); // Cached!

// Disconnect Redis when done
await sdk.disconnect();

Service Role Operations

import AuthSDK from "@mahdaad/auth-nodejs-sdk";

const sdk = new AuthSDK("http://localhost:3000");

// Create service role client
const serviceClient = sdk.createServiceRoleClient("your-service-role-key");

// Update user profile by ID
await serviceClient.updateProfileById("user-id", {
  first_name: "John",
  last_name: "Doe",
  name: "John Doe",
});

// Get multiple users by IDs
const users = await serviceClient.getUsersByIds(["id1", "id2"]);

// Get access token for a user
const tokenResult = await serviceClient.getAccessToken("user-id");

// Logout a user by ID
await serviceClient.logoutUserById("user-id");

// Create a user
const user = await serviceClient.createUser("[email protected]", "Password123", {
  first_name: "John",
  last_name: "Doe",
  name: "John Doe",
  mobile: "+1234567890",
  avatar: "https://example.com/avatar.jpg",
});
// Or create a user with only mobile
const user = await serviceClient.createUser(undefined, undefined, {
  first_name: "John",
  last_name: "Doe",
  name: "John Doe",
  mobile: "+1234567890",
  avatar: "https://example.com/avatar.jpg",
});

Token Management

// Set access token manually
sdk.setAccessToken("your-access-token");

// Use SDK with the token
const user = await sdk.user.getUser();

// Refresh token
const refreshResult = await sdk.auth.refreshToken("refresh-token");
sdk.setAccessToken(refreshResult.accessToken);

// Clear token
sdk.clearAccessToken();

OTP Authentication

// Request OTP
await sdk.auth.requestOTP("[email protected]", "+1234567890");

// Verify OTP
const result = await sdk.auth.verifyOTP(
  "[email protected]",
  "+1234567890",
  "123456"
);
sdk.setAccessToken(result.accessToken);

API Reference

AuthSDK Class

Constructor

new AuthSDK(baseURL?: string, redisOptions?: string | RedisOptions | null)
  • baseURL (string, optional): Base URL of the auth service API (default: "http://localhost:3000")
  • redisOptions (string | object, optional): Redis connection options

Methods

  • setAccessToken(token: string): void - Set access token for requests
  • clearAccessToken(): void - Clear access token
  • createServiceRoleClient(secretKey: string): ServiceRoleClient - Create service role client
  • disconnect(): Promise<void> - Disconnect Redis connection

AuthService

  • register(email: string, password: string): Promise<AuthResponse> - Register a new user
  • login(email: string, password: string): Promise<AuthResponse> - Login user
  • logout(accessToken?: string, refreshToken?: string): Promise<{ message: string }> - Logout user
  • refreshToken(refreshToken: string): Promise<{ accessToken: string }> - Refresh access token
  • requestOTP(email?: string, phone?: string): Promise<{ message: string }> - Request OTP code
  • verifyOTP(email: string | undefined, phone: string | undefined, code: string): Promise<AuthResponse> - Verify OTP code
  • getGoogleAuthUrl(): string - Get Google OAuth URL

UserService

  • getUser(accessToken?: string): Promise<User> - Get current user (with Redis caching)

ServiceRoleClient

  • updateProfileById(userId: string, profileData: UpdateProfileData): Promise<User> - Update user profile
  • getUsersByIds(userIds: string[]): Promise<User[]> - Get users by IDs
  • logoutUserById(userId: string): Promise<{ message: string }> - Logout user by ID
  • getAccessToken(userId: string): Promise<TokenResponse> - Get access token for user

Type Definitions

import type {
  AuthResponse,
  User,
  UpdateProfileData,
  TokenResponse,
  RedisOptions,
  RedisClient,
} from "@mahdaad/auth-nodejs-sdk";

Examples

See examples/basic-usage.js for comprehensive examples.

Environment Variables

AUTH_API_URL=http://localhost:3000
REDIS_URL=redis://localhost:6379
SERVICE_ROLE_KEY=your-service-role-key

Error Handling

All methods throw errors that can be caught:

try {
  await sdk.auth.login("[email protected]", "password");
} catch (error) {
  if (error instanceof Error) {
    console.error("Login failed:", error.message);
  }
}

Building from Source

# Install dependencies
npm install

# Build TypeScript
npm run build

# Watch mode
npm run build:watch

Publishing

# Build before publishing
npm run build

# Publish to npm
npm publish --access public

License

ISC

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.