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

@mridang/astro-auth

v1.0.0

Published

A comprehensive Auth.js integration for Astro applications with TypeScript support, framework-agnostic HTTP adapters, and role-based access control

Readme

Astro Auth.js

An Astro integration for Auth.js that provides seamless authentication with multiple providers, session management, and UI primitives that feel natural in Astro.

This integration brings the power and flexibility of Auth.js to Astro applications with full TypeScript support, SSR-friendly HTTP handling, and Astro-native patterns including integrations, endpoints, and components.

Why?

Modern web applications require robust, secure, and flexible authentication systems. While Auth.js provides excellent authentication capabilities, integrating it with Astro applications requires careful consideration of framework patterns, server-side rendering, and TypeScript integration.

However, a direct integration isn't always straightforward. Different types of applications or deployment scenarios might warrant different approaches:

  • Framework Integration: Auth.js operates at the HTTP level, while Astro uses integrations, endpoints, and components. A proper integration should bridge this gap by providing Astro-native primitives for authentication and authorization while maintaining the full Auth.js ecosystem compatibility.
  • HTTP Request Handling: Astro’s server output and adapters (Node, Vercel, etc.) require clean request handling and route injection. Teams need a unified approach that maintains performance while providing seamless Auth.js integration.
  • Session and Request Lifecycle: Proper session handling in Astro requires SSR-friendly utilities and components that work across server-rendered pages and client interactions.
  • Route Protection & UI: Many applications need fine-grained authorization beyond simple authentication. This calls for cohesive building blocks: server utilities, client helpers, and drop-in UI components.

This integration, @mridang/astro-auth, aims to provide the flexibility to handle such scenarios. It allows you to leverage the full Auth.js ecosystem while maintaining Astro best practices, ultimately leading to a more effective and less burdensome authentication implementation.

Installation

Install using NPM by using the following command:

npm install @mridang/astro-auth @auth/core

Usage

To use this integration, add the @mridang/astro-auth integration to your Astro application. The integration provides authentication infrastructure with configurable endpoints, SSR utilities, and components.

You'll need to configure it with your Auth.js providers and options. The integration will then be available throughout your application via Astro’s integration system.

First, add the integration to your Astro config:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import authAstro from '@mridang/astro-auth';

export default defineConfig({
  output: 'server',
  integrations: [
    authAstro({
      // Optional:
      // prefix: '/api/auth',
      // configFile: './auth.config.ts'
    }),
  ],
});
// auth.config.ts
import { defineConfig } from '@mridang/astro-auth';
import Google from '@auth/core/providers/google';

export default defineConfig({
  providers: [
    Google({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
  secret: process.env.AUTH_SECRET,
  trustHost: true,
});

Using the Authentication System

The integration provides several functions and hooks for handling authentication:

Functions and Hooks:

  • getSession(request, config?): Retrieves the current Auth.js session (SSR)
  • <Auth>: Render-prop component that provides the current session to children
  • <SignIn provider="...">: Drop-in button component for starting sign-in
  • <SignOut>: Drop-in button component for signing out
  • signIn(provider, options?, authParams?): Client helper for programmatic sign-in
  • signOut(options?): Client helper for programmatic sign-out

Basic Usage:

---
// src/pages/index.astro
import { getSession } from '@mridang/astro-auth/server';
import type { Session } from '@auth/core/types';

const session = await getSession(Astro.request);
---

<html>
  <body>
    {session ? (
      <>
        <p>Welcome {session.user?.name}</p>
        <a href="/api/auth/signout">Sign out</a>
      </>
    ) : (
      <a href="/api/auth/signin">Sign in</a>
    )}
  </body>
</html>

Prefer using components? Use the built-ins for a richer experience:

---
// src/pages/index.astro
import type { Session } from '@auth/core/types';
import { Auth, SignIn, SignOut } from '@mridang/astro-auth/components';
---

<Auth>
  {(session: Session | null) => (
    <>
      {session ? (
        <>
          <SignOut>Sign out</SignOut>
          <p>Logged in as {session.user?.name}</p>
        </>
      ) : (
        <SignIn provider="github">Sign in with GitHub</SignIn>
      )}
    </>
  )}
</Auth>

Prefer client helpers? Use inline script tags:

---
---

<html>
  <body>
    <button id="login">Login</button>
    <button id="logout">Logout</button>

    <script>
      const { signIn, signOut } = await import("@mridang/astro-auth/client");
      document.querySelector("#login").onclick = () => signIn("github");
      document.querySelector("#logout").onclick = () => signOut();
    </script>
  </body>
</html>
Example: Advanced Configuration with Multiple Providers

This example shows how to use the integration with multiple Auth.js providers and custom session configuration:

// auth.config.ts
import { defineConfig } from '@mridang/astro-auth';
import Google from '@auth/core/providers/google';
import GitHub from '@auth/core/providers/github';

export default defineConfig({
  providers: [
    Google({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
    GitHub({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
    }),
  ],
  secret: process.env.AUTH_SECRET,
  trustHost: true,
  session: {
    strategy: 'jwt',
    maxAge: 30 * 24 * 60 * 60, // 30 days
  },
  callbacks: {
    async jwt({ token, user }) {
      if (user) (token as any).roles = (user as any).roles;
      return token;
    },
    async session({ session, token }) {
      (session.user as any).roles = (token as any).roles as
        | string[]
        | undefined;
      return session;
    },
  },
});

Known Issues

  • SSR & Adapter Required: The integration requires Astro’s server output with an adapter (e.g., @astrojs/node, Vercel, etc.). Ensure output: 'server' is set and an adapter is configured in astro.config.mjs.
  • Environment Configuration: The integration relies on AUTH_SECRET and, in many hosting scenarios, AUTH_TRUST_HOST. Ensure these are correctly set in your environment for production.
  • Callback URLs: OAuth providers must be configured with the correct callback URL: [origin]/api/auth/callback/[provider] (or your custom prefix).
  • Type Augmentation: If you attach additional properties (e.g., roles) to the Auth.js user object, extend your app’s types accordingly so consumers of session.user remain type-safe.
  • Redirect Semantics: OAuth providers expect real browser navigations during sign-in. The client helpers handle this for you—avoid manual fetch() calls to provider endpoints unless you know you need credential/email flows.

Useful links

  • Auth.js: The authentication library that this integration is built upon.
  • Astro: The framework this integration targets.
  • Auth.js Providers: Complete list of supported authentication providers.

Contributing

If you have suggestions for how this integration could be improved, or want to report a bug, open an issue - we'd love all and any contributions.

License

Apache-2.0