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

@studiosonrai/nestjs-azure-auth

v1.1.2

Published

Reusable NestJS package for Azure AD SSO authentication. Provides Passport strategy, guards, decorators, and a dynamic module that accepts pluggable user-resolution functions (no direct DB coupling).

Readme

@studiosonrai/nestjs-azure-auth

NestJS package for Azure AD SSO authentication. Validates bearer tokens, resolves the user via a pluggable callback, and attaches it to req.user — no fixed user shape, zero DB coupling.

Installation

npm install @studiosonrai/nestjs-azure-auth
npm install @nestjs/passport passport passport-azure-ad

Usage

1. Register

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AzureAuthModule } from '@studiosonrai/nestjs-azure-auth';

@Module({
  imports: [
    ConfigModule,
    AzureAuthModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        clientId: config.get('AZURE_AD_CLIENT_ID'),
        tenantId: config.get('AZURE_AD_TENANT_ID'),
        findUserByEmail: async (email) => myService.findByEmail(email),
      }),
    }),
  ],
})
export class AuthModule {}

2. findUserByEmail

Receives the normalised (lowercased, trimmed) email from the token. Return a user object or null to reject with 401. The shape is entirely up to you.

3. mapUser (optional)

Transform the user before attaching to req.user. Receives the raw user from findUserByEmail and the decoded Azure AD token payload (for access to tid, oid, etc.).

AzureAuthModule.forRoot({
  clientId: '...',
  tenantId: '...',
  findUserByEmail: async (email) => db.findUser(email),
  mapUser: (user, token) => ({
    id: user.id,
    email: user.email,
    tenantId: token.tid,
    azureUserId: token.oid,
  }),
});

If omitted, the raw result of findUserByEmail is used directly.

4. Protect routes

import { Controller, Get, UseGuards } from '@nestjs/common';
import {
  AzureAuthGuard,
  RolesGuard,
  Roles,
  Public,
  CurrentUser,
} from '@studiosonrai/nestjs-azure-auth';

@Controller('profile')
export class ProfileController {
  @Get()
  @UseGuards(AzureAuthGuard)
  getProfile(@CurrentUser() user: { id: number; email: string }) {
    return user;
  }

  @Get('admin')
  @UseGuards(AzureAuthGuard, RolesGuard)
  @Roles('admin')
  adminOnly(@CurrentUser() user: { id: number; email: string; roles: string[] }) {
    return { message: `Welcome admin ${user.email}` };
  }

  @Get('health')
  @Public()
  health() {
    return { ok: true };
  }
}

API

| Export | Kind | Description | |--------|------|-------------| | AzureAuthModule | Module | Dynamic module with forRootAsync. | | AzureAuthGuard | Guard | Validates bearer token via Azure AD strategy. Skips @Public() routes. | | RolesGuard | Guard | Checks req.user.roles against roles set by @Roles(). | | @Roles(...) | Decorator | Marks route with required roles. | | @Public() | Decorator | Bypasses AzureAuthGuard. | | @CurrentUser(prop?) | Param decorator | Injects user (or a single property) from req.user. |

Options

| Option | Type | Description | |--------|------|-------------| | clientId | string | Azure AD app (client) ID. Required. | | tenantId | string | Azure AD tenant ID. Required. | | findUserByEmail | (email) => Promise<unknown \| null> | User resolver. Required. | | mapUser | (user, token) => unknown | Optional transform. Receives user + raw token payload. | | logger | (context: string) => LoggerService | Optional logger factory. Routes package logs through your app's logger (winston, etc.). Silent by default. |