pikku-auth-js

$npx mdskill add pikkujs/pikku/pikku-auth-js

Integrate Auth.js OAuth and session management into Pikku apps.

  • Generates secure authentication handlers and HTTP route contracts.
  • Depends on Auth.js core services and singleton service patterns.
  • Triggers on requests involving OAuth providers or Auth.js configuration.
  • Returns ready-to-use functions for signin, signout, and callback routes.

SKILL.md

.github/skills/pikku-auth-jsView on GitHub ↗
---
name: pikku-auth-js
description: 'Use when integrating Auth.js (NextAuth) with a Pikku app. Covers createAuthHandler, createAuthRoutes, and Auth.js configuration.
TRIGGER when: code uses createAuthHandler, createAuthRoutes, user asks about Auth.js, NextAuth, OAuth providers, or @pikku/auth-js.
DO NOT TRIGGER when: user asks about JWT middleware (use pikku-security) or custom session services (use pikku-services).'
---

# Pikku Auth.js Integration

`@pikku/auth-js` provides [Auth.js](https://authjs.dev/) integration for Pikku apps, handling OAuth providers, session management, and auth routes.

## Installation

```bash
yarn add @pikku/auth-js @auth/core
```

## API Reference

### `createAuthHandler(config)`

Creates a Pikku function that handles all Auth.js routes (signin, signout, callback, etc.):

```typescript
import { createAuthHandler } from '@pikku/auth-js'

const authHandler = createAuthHandler(
  config: AuthConfig | ((services: CoreSingletonServices) => AuthConfig | Promise<AuthConfig>)
)
// Returns: { func: CorePikkuFunctionSessionless }
```

The config can be static or a factory function that receives singleton services (useful for dynamic provider configuration).

### `createAuthRoutes(config, basePath?)`

Creates HTTP route contracts for Auth.js endpoints:

```typescript
import { createAuthRoutes } from '@pikku/auth-js'

const authRoutes = createAuthRoutes(
  config: AuthConfig | ((services) => AuthConfig | Promise<AuthConfig>),
  basePath?: string  // default: '/auth'
)
// Returns: HTTPRouteContract<HTTPRouteMap>
```

## Usage Patterns

### Basic Setup

```typescript
import { createAuthHandler, createAuthRoutes } from '@pikku/auth-js'
import GitHub from '@auth/core/providers/github'

const authConfig = {
  providers: [
    GitHub({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET,
    }),
  ],
}

const authHandler = createAuthHandler(authConfig)
const authRoutes = createAuthRoutes(authConfig)
```

### Dynamic Config with Services

```typescript
const authHandler = createAuthHandler(async (services) => {
  const githubSecret = await services.secrets.getSecretJSON('github-oauth')
  return {
    providers: [
      GitHub({
        clientId: githubSecret.clientId,
        clientSecret: githubSecret.clientSecret,
      }),
    ],
  }
})
```

### Wiring Auth Routes

```typescript
import { wireHTTPRoute } from '@pikku/core/http'

// Auth routes are automatically wired when passed to your HTTP runner
const routes = [
  ...authRoutes,
  // ...your other routes
]
```

More from pikkujs/pikku

SkillDescription
pikku-addon'Use when creating or consuming reusable function packages (addons) in Pikku. Covers wireAddon, addon(), pikkuAddonServices, pikkuAddonWireServices, addon package structure, and cross-project function sharing.
pikku-ai-agent'Use when building AI agents, chatbots, or LLM-powered assistants with Pikku. Covers pikkuAIAgent, tool registration, memory, streaming, and agent invocation.
pikku-ai-vercel'Use when setting up AI agent execution with the Vercel AI SDK in a Pikku app. Covers VercelAIAgentRunner for streaming and non-streaming AI agent steps.
pikku-ai-voice'Use when adding voice input (speech-to-text) or voice output (text-to-speech) to AI agents in a Pikku app. Covers voiceInput/voiceOutput middleware hooks and STT/TTS service interfaces.
pikku-aws'Use when setting up AWS services (S3, SQS, Secrets Manager) in a Pikku app. Covers S3Content for file storage, SQSQueueService for queues, and AWSSecrets for secret management.
pikku-backblaze'Use when setting up Backblaze B2 file storage in a Pikku app. Covers B2Content for file uploads, downloads, and signed URLs.
pikku-cli'Use when building CLI commands with Pikku. Covers wireCLI, pikkuCLICommand, subcommands, options, parameters, custom renderers, and nested command groups.
pikku-concepts'Foundational guide to Pikku framework concepts. Use this skill when working with any Pikku codebase, starting a new Pikku project, or migrating a backend to Pikku. Covers the core mental model, function types, project structure, code generation, testing, and how Pikku maps to traditional backend patterns.
pikku-config'Use when managing secrets, environment variables, config, or OAuth2 credentials in a Pikku app. Covers wireSecret, wireVariable, wireOAuth2Credential, and typed config access.
pikku-cron'Use when adding scheduled tasks, recurring jobs, or cron-based automation to a Pikku app. Covers wireScheduler, cron expressions, scheduled task wire object, and scheduler middleware.