sf-auth-middleware-nextjs/README.md

1.5 KiB

sf-auth-nextjs-middleware

Nextjs middleware and App Router callback handler for sf-auth.

Install

npm install https://forgejo.snazzyfellas.com/snazzyfellas-public-libs/sf-auth-middleware-nextjs

Usage

Middleware

Create a middleware that redirects unauthenticated users to the sf-auth authenticate endpoint.

// middleware.ts
import { createSfAuthMiddleware } from "sf-auth-nextjs-middleware";

export const middleware = createSfAuthMiddleware(
  "https://your-site.com/auth_callback"
);

Callback route

Create an App Router route to handle the sf-auth callback, validate the one-time key, set cookies, and redirect to the desired page.

// app/auth_callback/route.ts
import { createSfAuthCallbackRoute } from "sf-auth-nextjs-middleware";

export const GET = createSfAuthCallbackRoute({
  redirectTo: "/user_info"
});

Configuration

Both helpers accept optional configuration to customize cookie names and endpoints.

import {
  createSfAuthMiddleware,
  createSfAuthCallbackRoute
} from "sf-auth-nextjs-middleware";

export const middleware = createSfAuthMiddleware(
  "https://your-site.com/auth_callback",
  {
    cookieNames: {
      userId: "sf_user_id",
      username: "sf_username"
    }
  }
);

export const GET = createSfAuthCallbackRoute({
  redirectTo: "/user_info",
  cookieNames: {
    userId: "sf_user_id",
    username: "sf_username"
  },
  validateEndpoint: "https://snazzyfellas.com/api/redirect/validate"
});