-
Notifications
You must be signed in to change notification settings - Fork 1
feat(express): add express integration package (@aura-stack/express) #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
309fa75
feat(express): add express integratoin package
halvaradop 55dad13
feat(express): infer user type from auth instance
halvaradop 67554ce
chore: add tests and apply coderabbit
halvaradop e0fd6d8
refactor(express): update the headers when a session is retrieved
halvaradop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,55 @@ | ||
| <div align="center"> | ||
|
|
||
| <h1>Aura Auth + Express</h1> | ||
|
|
||
| **Integration Example App** | ||
|
|
||
| [Official Docs](https://aura-stack-auth.vercel.app/docs/oauth) · [Express](https://expressjs.com/) | ||
|
|
||
| </div> | ||
|
|
||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| This project demonstrates Aura Auth authentication using [OAuth 2.0 providers](https://aura-stack-auth.vercel.app/docs/) in an [Express](https://expressjs.com/) application. | ||
|
|
||
|
|
||
| This project demonstrates **Aura Auth** authentication using [OAuth 2.0 providers](https://aura-stack-auth.vercel.app/docs/) in an [Express](https://expressjs.com/) application. | ||
|
|
||
| This integration utilizes the dedicated [`@aura-stack/express`](../../packages/express) package, which provides standard middlewares and first-class TypeScript support for Express applications. | ||
|
|
||
| ## Features Demo | ||
|
|
||
| - **Type-safe Middleware**: Uses `withAuth` to protect routes and infer session shapes. | ||
| - **Unified Auth Handler**: Reuses framework-agnostic core logic via `toHandler`. | ||
| - **Global Context**: Demonstrates how `res.locals.session` is automatically populated and typed. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| You can run the documentation site locally for development or contribution. | ||
|
|
||
|
|
||
| ### From the repository root | ||
|
|
||
| ```bash | ||
| pnpm dev --filter=express | ||
| ``` | ||
|
|
||
| ### Or manually from the app directory | ||
|
|
||
| ```bash | ||
| cd apps/express | ||
| pnpm install | ||
| pnpm dev | ||
| ``` | ||
|
|
||
| Once started, open your browser at `http://localhost:3000` to view the app. | ||
|
|
||
| ## Documentation | ||
|
|
||
| Visit the [**official documentation website**](https://aura-stack-auth.vercel.app). | ||
|
|
||
| ## License | ||
|
|
||
| Licensed under the [MIT License](LICENSE). © [Aura Stack](https://github.com/aura-stack-ts) | ||
|
|
||
| --- | ||
|
|
||
| <p align="center"> | ||
| Made with ❤️ by <a href="https://github.com/aura-stack-ts">Aura Stack team</a> | ||
| </p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import express, { type Express } from "express" | ||
| import { toHandler, withAuth } from "@/lib/auth.js" | ||
|
|
||
| const app: Express = express() | ||
|
|
||
| app.use(express.json()) | ||
| app.use(express.urlencoded({ extended: true })) | ||
|
|
||
| app.all("/api/auth/*", toHandler) | ||
|
|
||
| app.get("/api/protected", withAuth, (_, res) => { | ||
| if (!res.locals.session) { | ||
| return res.status(401).json({ message: "Unauthorized" }) | ||
| } | ||
| return res.json({ | ||
| message: "You have access to this protected resource.", | ||
| session: res.locals.session, | ||
| }) | ||
| }) | ||
|
|
||
| export { app } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { app } from "@/server.js" | ||
| import { app } from "@/app.js" | ||
|
|
||
| const PORT = process.env.PORT ?? 3000 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { createAuth, builtInOAuthProviders, type BuiltInOAuthProvider } from "@aura-stack/express" | ||
|
|
||
| export const oauth = Object.keys(builtInOAuthProviders) as BuiltInOAuthProvider[] | ||
|
|
||
| export const auth = createAuth({ | ||
| oauth: ["github"], | ||
| basePath: "/api/auth", | ||
| }) | ||
|
|
||
| export const { api, jose, toHandler, withAuth } = auth |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,10 @@ | ||
| { | ||
| "extends": "@aura-stack/tsconfig/tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist", | ||
| "paths": { | ||
| "@/*": ["./src/*"], | ||
| "@test/*": ["./test/*"] | ||
| }, | ||
| "baseUrl": "./" | ||
| "@/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "include": ["src", "test"] | ||
| "include": ["src"], | ||
| "exclude": ["dist", "node_modules"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Changelog - `@aura-stack/express` | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
||
| --- | ||
|
|
||
| ## [Unreleased] | ||
|
|
||
| ### Added | ||
|
|
||
| - Introduced a seamless Express integration package that encapsulates the core authentication logic into middleware and handlers for session management and authentication flows. [#138](https://github.com/aura-stack-ts/auth/pull/138) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| <div align="center"> | ||
|
|
||
| <h1><b>@aura-stack/express</b></h1> | ||
|
|
||
| **Type-safe Express middleware and utilities for the Aura Stack authentication library** | ||
|
|
||
| [](https://www.npmjs.com/package/@aura-stack/express) | ||
| [](https://jsr.io/@aura-stack/express) | ||
| [](https://opensource.org/licenses/MIT) | ||
|
|
||
| [Official Docs](https://aura-stack-auth.vercel.app/docs) · [Express Package Docs](https://aura-stack-auth.vercel.app/docs/packages/express) | ||
|
|
||
| </div> | ||
|
|
||
| ## Overview | ||
|
|
||
| `@aura-stack/express` provides a seamless integration layer for **Express** applications using **Aura Auth**. It encapsulates the core authentication logic into standard Express middlewares, ensuring that your session state is correctly typed and accessible throughout your application's middleware chain. | ||
|
|
||
| By leveraging TypeScript's global augmentation and middleware inference, it provides a "zero-effort" typed experience for your protected routes. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Standard Middleware** — Use `withAuth` as a standard middleware in your route definitions. | ||
| - **Deep Type Inference** — Automatically infers your custom `User` and `Session` shapes from the initial configuration. | ||
| - **Global Augmentation** — Adds first-class support for `res.locals.session` directly to Express types. | ||
| - **Framework-Agnostic Core** — Bridge Web Request/Response API handlers to Express without sacrificing performance. | ||
| - **Typed Propagation** — Middleware correctly propagates types to subsequent handlers in the route chain. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pnpm add @aura-stack/express | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Configure Auth | ||
|
|
||
| Create your authentication instance. This typically goes in a shared file like `lib/auth.ts`. | ||
|
|
||
| ```tsx | ||
| import { createAuth } from "@aura-stack/express" | ||
|
|
||
| export const auth = createAuth({ | ||
| oauth: ["github"], | ||
| session: { | ||
| strategy: "jwt", | ||
| maxAge: 30 * 24 * 60 * 60, // 30 days | ||
| }, | ||
| }) | ||
|
|
||
| export const { toHandler, withAuth } = auth | ||
| ``` | ||
|
|
||
| ### 2. Mount Auth Endpoints | ||
|
|
||
| Mount the authentication endpoints on your desired base path (default: `/api/auth`). | ||
|
|
||
| ```tsx | ||
| import express from "express" | ||
| import { toHandler } from "./lib/auth" | ||
|
|
||
| const app = express() | ||
|
|
||
| // All Aura Auth routes (sign-in, sign-out, session, etc.) | ||
| app.all("/api/auth/*", toHandler) | ||
| ``` | ||
|
|
||
| ### 3. Protect Your Routes | ||
|
|
||
| Use the `withAuth` middleware to protect routes. The session will be automatically available and **fully typed** in `res.locals.session`. | ||
|
|
||
| ```tsx | ||
| import { withAuth } from "./lib/auth" | ||
|
|
||
| app.get("/api/protected", withAuth, (req, res) => { | ||
| // session is automatically available and typed! | ||
| const session = res.locals.session | ||
|
|
||
| if (!session) { | ||
| return res.status(401).json({ error: "Unauthorized" }) | ||
| } | ||
|
|
||
| // TypeScript knows about session.user.name, email, etc. | ||
| res.json({ message: `Hello, ${session.user.name}!` }) | ||
| }) | ||
| ``` | ||
|
|
||
| ## Documentation | ||
|
|
||
| Visit the [**official documentation website**](https://aura-stack-auth.vercel.app) for more detailed guides and API references. | ||
|
|
||
| ## License | ||
|
|
||
| Licensed under the [MIT License](../../LICENSE). © [Aura Stack](https://github.com/aura-stack-ts) | ||
|
|
||
| --- | ||
|
|
||
| <p align="center"> | ||
| Made with ❤️ by <a href="https://github.com/aura-stack-ts">Aura Stack team</a> | ||
| </p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "@aura-stack/express", | ||
| "version": "0.0.0", | ||
| "license": "MIT", | ||
| "tasks": { | ||
| "dev": "deno run --watch src/index.ts" | ||
| }, | ||
| "exports": { | ||
| ".": "./src/index.ts", | ||
| "./oauth": "./src/oauth/index.ts" | ||
| }, | ||
| "imports": { | ||
| "@/": "./src/" | ||
| }, | ||
| "publish": { | ||
| "include": ["src/**/*.ts", "README.md", "CHANGELOG.md"] | ||
| }, | ||
| "exclude": ["dist", "node_modules"] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.