Skip to content

GittieLabs/nextjs-fastapi-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@gittielabs/nextjs-fastapi-auth

A comprehensive, production-ready authentication and authorization library for Next.js + FastAPI applications with Supabase.

npm version PyPI version License: MIT

Features

  • Unified Authentication - Consistent Supabase JWT validation across frontend and backend
  • Multi-Tenant Support - Built-in organization context with subdomain routing
  • Role-Based Access Control - Admin, owner, and custom role management
  • Protected Routes - Middleware and decorators for route protection
  • WebSocket Authentication - Secure WebSocket connections with token validation
  • Type-Safe - Full TypeScript and Python type definitions
  • Production-Ready - Connection pooling, caching, and error handling built-in

Quick Start

Installation

# Frontend (Next.js)
npm install @gittielabs/nextjs-fastapi-auth

# Backend (FastAPI)
pip install gittielabs-nextjs-fastapi-auth

Frontend Setup

// middleware.ts
import { createAuthMiddleware } from '@gittielabs/nextjs-fastapi-auth/middleware'

export const middleware = createAuthMiddleware({
  protectedRoutes: ['/dashboard', '/admin'],
  adminRoutes: ['/admin'],
  subdomainAuth: true
})

export const config = {
  matcher: ['/((?!_next/static|_next/image|favicon.ico).*)']
}
// app/api/jobs/route.ts
import { authenticatedFetch } from '@gittielabs/nextjs-fastapi-auth/client'

export async function GET() {
  const response = await authenticatedFetch('/api/v1/jobs')
  return response
}

Backend Setup

# main.py
from fastapi import FastAPI, Depends
from gittielabs_auth.middleware import create_auth_middleware
from gittielabs_auth.dependencies import require_auth, require_organization
from gittielabs_auth.models import AuthUser

app = FastAPI()

# Add authentication middleware
app.add_middleware(
    create_auth_middleware(
        require_auth_paths=["/api/v1/"],
        public_paths=["/health", "/docs"]
    )
)

# Protected endpoint
@app.get("/api/v1/jobs")
async def get_jobs(
    user: AuthUser = Depends(require_auth),
    org_id: str = Depends(require_organization)
):
    return {"user": user.email, "organization": org_id}

Documentation

Examples

Architecture

Frontend (Next.js)

  • Middleware - Route protection and subdomain handling
  • Client Utils - Authenticated fetch with automatic token injection
  • Server Utils - API route helpers with admin validation
  • Hooks - React hooks for auth state and session management
  • WebSocket - Authenticated WebSocket connections

Backend (FastAPI)

  • Middleware - Request authentication with Supabase JWT validation
  • Dependencies - FastAPI dependencies for route protection
  • Services - Auth services with connection pooling and caching
  • Context - Organization and admin context extraction
  • WebSocket - WebSocket authentication and connection management

Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

License

MIT © GittieLabs

Support

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •