From 60bdb2280fcffedb6aa23c2528f61c0ab8e2a93d Mon Sep 17 00:00:00 2001
From: John | Elite Encoder
Date: Sat, 18 Apr 2026 18:56:40 -0400
Subject: [PATCH 1/5] feat: integrate PymtHouse authentication flow and enhance
session management
Added support for PymtHouse integration, including new environment variables for issuer URL and client IDs. Implemented device login approval and completion routes, along with user session management. Updated the login page to handle device flow and improved the AuthContext for better state management. Enhanced the API for token management and usage tracking, ensuring a seamless user experience across the application.
Co-Authored-By: Claude Opus 4.6 (1M context)
---
.env.example | 14 +
app/(studio)/studio/header-qa/page.tsx | 2 +-
.../studio/device-approved/page.tsx | 45 +++
app/(studio-auth)/studio/login/page.tsx | 8 +-
app/api/auth/device/complete/route.ts | 66 ++++
app/api/auth/initiate-login/route.ts | 45 +++
app/api/auth/login/route.ts | 107 ++++++
app/api/auth/logout/route.ts | 10 +
app/api/auth/me/route.ts | 27 ++
app/api/tokens/[id]/route.ts | 38 ++
app/api/tokens/route.ts | 101 +++++
app/api/usage/route.ts | 174 +++++++++
components/studio/AuthContext.tsx | 93 +++--
components/studio/LoginPage.tsx | 109 ++++--
components/studio/StudioHeader.tsx | 10 +-
components/studio/settings/AccountTab.tsx | 2 +-
components/studio/settings/ApiKeysTab.tsx | 306 +++++++++++----
components/studio/settings/UsageTab.tsx | 108 +++++-
lib/pymthouse/README.md | 203 ++++++++++
lib/pymthouse/client.ts | 351 ++++++++++++++++++
lib/pymthouse/discovery.ts | 76 ++++
lib/pymthouse/errors.ts | 45 +++
lib/pymthouse/format.ts | 24 ++
lib/pymthouse/index.ts | 20 +
lib/pymthouse/server.ts | 41 ++
lib/pymthouse/types.ts | 115 ++++++
lib/session.ts | 326 ++++++++++++++++
lib/studio-auth.ts | 63 ++++
package.json | 1 +
pnpm-lock.yaml | 17 +-
30 files changed, 2378 insertions(+), 169 deletions(-)
create mode 100644 app/(studio-auth)/studio/device-approved/page.tsx
create mode 100644 app/api/auth/device/complete/route.ts
create mode 100644 app/api/auth/initiate-login/route.ts
create mode 100644 app/api/auth/login/route.ts
create mode 100644 app/api/auth/logout/route.ts
create mode 100644 app/api/auth/me/route.ts
create mode 100644 app/api/tokens/[id]/route.ts
create mode 100644 app/api/tokens/route.ts
create mode 100644 app/api/usage/route.ts
create mode 100644 lib/pymthouse/README.md
create mode 100644 lib/pymthouse/client.ts
create mode 100644 lib/pymthouse/discovery.ts
create mode 100644 lib/pymthouse/errors.ts
create mode 100644 lib/pymthouse/format.ts
create mode 100644 lib/pymthouse/index.ts
create mode 100644 lib/pymthouse/server.ts
create mode 100644 lib/pymthouse/types.ts
create mode 100644 lib/session.ts
create mode 100644 lib/studio-auth.ts
diff --git a/.env.example b/.env.example
index d3ba41f..3f64356 100644
--- a/.env.example
+++ b/.env.example
@@ -5,3 +5,17 @@ MAILCHIMP_TAG=v2 Website Signups
# The Graph (optional — falls back to hardcoded values)
THEGRAPH_API_KEY=
+
+# PymtHouse integration (required for Studio auth/device flow)
+# Issuer must be the full OIDC issuer URL, e.g. http://localhost:3001/api/v1/oidc
+PYMTHOUSE_ISSUER_URL=
+# Public OIDC client id (app_...)
+PYMTHOUSE_PUBLIC_CLIENT_ID=
+# Confidential helper client id (m2m_...)
+PYMTHOUSE_M2M_CLIENT_ID=
+# Confidential helper secret (pmth_cs_...)
+PYMTHOUSE_M2M_CLIENT_SECRET=
+
+# Website session signing secret
+# Generate with: openssl rand -base64 32
+LP_SESSION_SECRET=
diff --git a/app/(studio)/studio/header-qa/page.tsx b/app/(studio)/studio/header-qa/page.tsx
index cda6f97..cfa92b5 100644
--- a/app/(studio)/studio/header-qa/page.tsx
+++ b/app/(studio)/studio/header-qa/page.tsx
@@ -34,7 +34,7 @@ export default function HeaderQaPage() {
))}
+ {deviceFlow && (
+
+ Complete sign-in to approve your pending device login.
+
+ )}
+ {loginError && (
+
+ We could not continue the device authorization flow. Please sign
+ in and try again.
+
+ )}
@@ -167,6 +200,7 @@ export default function LoginPage() {