Files
tweakcn/middleware.ts
T
Sahaj Jain 4c41596712 AI theme generation just got 10x better (#165)
* feat: Add polar for tweakcn-plus checkout

* migrate to polar nextjs for more control

* chore: save closable chat suggestions state as a preference in local storage

* chore: improve color contrast in user UI message

* feat: add pro features components

* chore: update TextArea styles

* feat: adjust theme generation types, functions and API to support adding an image

* feat: AI components now support and display images

* feat: add support for multiple images

* feat: add support for multiple images to components

* feat: support image only prompts and clean up API code

* chore: save closable chat suggestions state as a preference in local storage

* chore: improve color contrast in user UI message

* feat: add pro features components

* chore: update TextArea styles

* feat: adjust theme generation types, functions and API to support adding an image

* feat: AI components now support and display images

* feat: add support for multiple images

* feat: add support for multiple images to components

* feat: support image only prompts and clean up API code

* add checkout link support

* add ai-usage schema and actions

* feat: dashboard: add AI usage stats with bar chart

* feat: enforce subscription limits and improved error handling for AI theme generation

* feat: Integrate alert-banner with actual usage

* fix: pricing navigation

* feat: Checkout link generation

* feat: add quick action to create a theme from an image in ai page

* feat: add drag and drop for images

* chore: move Image Upload constant vars and update imports

* feat: improve feedback while theme is being generated

* feat: Add pricing page UI

* refactor: Dashboard migration to Settings page

* feat: improve pricing page

* feat: add auth check in checkout and refactor implementation for consistency and typesafety

* refactor: adjust code and API to follow new types and useQuery hook typesafety

* refactor: alert banner displays correctly and uses the new subscription hook

* feat: improve UI feedback for Pro users

* feat: upload images by pasting (cmd/crtl + v)

* feat: add Go Pro dialog, and global store (no implemented in app yet)

* chore(WIP): scaffold of Pro subscription modal show logic

* feat: Add openProModal logic to handleGenerateTheme in chat-interface

* chore: save closable chat suggestions state as a preference in local storage

* chore: improve color contrast in user UI message

* feat: add pro features components

* chore: update TextArea styles

* feat: adjust theme generation types, functions and API to support adding an image

* feat: AI components now support and display images

* feat: add support for multiple images

* feat: add support for multiple images to components

* feat: support image only prompts and clean up API code

* add ai-usage schema and actions

* feat: dashboard: add AI usage stats with bar chart

* feat: enforce subscription limits and improved error handling for AI theme generation

* feat: Integrate alert-banner with actual usage

* fix: pricing navigation

* feat: Checkout link generation

* feat: add quick action to create a theme from an image in ai page

* feat: add drag and drop for images

* chore: move Image Upload constant vars and update imports

* feat: improve feedback while theme is being generated

* feat: Add pricing page UI

* refactor: Dashboard migration to Settings page

* feat: improve pricing page

* feat: add auth check in checkout and refactor implementation for consistency and typesafety

* refactor: adjust code and API to follow new types and useQuery hook typesafety

* refactor: alert banner displays correctly and uses the new subscription hook

* feat: improve UI feedback for Pro users

* feat: upload images by pasting (cmd/crtl + v)

* feat: add Go Pro dialog, and global store (no implemented in app yet)

* chore(WIP): scaffold of Pro subscription modal show logic

* feat: Add openProModal logic to handleGenerateTheme in chat-interface

* refactor: follow naming convention

* chore: refactor components and layout/pages for consistency

* fix: enforce auth checks or redirect in /settings routes

* chore: update Go Pro dialog

* refactor: create reusable theme toggle component

* feat: add Get Pro dialog to header

* chore: rename GoPro dialog to GetPro and update imports

* feat: add AI generation section to Home page and adjust Get Pro dialog

* chore: save closable chat suggestions state as a preference in local storage

* chore: improve color contrast in user UI message

* feat: add pro features components

* chore: update TextArea styles

* feat: adjust theme generation types, functions and API to support adding an image

* feat: AI components now support and display images

* feat: add support for multiple images

* feat: add support for multiple images to components

* feat: support image only prompts and clean up API code

* add ai-usage schema and actions

* feat: dashboard: add AI usage stats with bar chart

* feat: enforce subscription limits and improved error handling for AI theme generation

* feat: Integrate alert-banner with actual usage

* fix: pricing navigation

* feat: Checkout link generation

* feat: add quick action to create a theme from an image in ai page

* feat: add drag and drop for images

* chore: move Image Upload constant vars and update imports

* feat: improve feedback while theme is being generated

* feat: Add pricing page UI

* refactor: Dashboard migration to Settings page

* feat: improve pricing page

* feat: add auth check in checkout and refactor implementation for consistency and typesafety

* refactor: adjust code and API to follow new types and useQuery hook typesafety

* refactor: alert banner displays correctly and uses the new subscription hook

* feat: improve UI feedback for Pro users

* feat: upload images by pasting (cmd/crtl + v)

* feat: add Go Pro dialog, and global store (no implemented in app yet)

* chore(WIP): scaffold of Pro subscription modal show logic

* feat: Add openProModal logic to handleGenerateTheme in chat-interface

* refactor: follow naming convention

* chore: refactor components and layout/pages for consistency

* fix: enforce auth checks or redirect in /settings routes

* chore: update Go Pro dialog

* refactor: create reusable theme toggle component

* feat: add Get Pro dialog to header

* chore: rename GoPro dialog to GetPro and update imports

* feat: add AI generation section to Home page and adjust Get Pro dialog

* feat: Add customer portal

* feat: add chat input draft persistence (not images)

* styles: improve styles for ai generation and alert banner defaults

* feat: Send whole chat context to AI

* fix: silly typo lol

* feat: support images persistence and sync

* feat: edit message (text, mentions and images)

* fix: add eslint-disable for exhaustive-deps warnings

* chore: fix build error & improve text and styles in suggested prompts

* refactor: handle session and subscription checks and handle post login actions

* migrate to turbopack

* refactor: use reducer builder pattern to sync ai-draft-store with image-uploader

* fix: ai-usage computation: account for abort

* feat: Theme limit handling for pro tier

* refactor: remove duplicate code from ai-chat-form and chat-input

* feat: Add faq to pricing page

* fix: /dashboard navigations -> /settings

* feat: update ui for success page

* feat: Migrate ai chat and draft message store to idb-keyval

* feat: move subscription details in-house

* generate migration

* fix: build err

* styles: improve Message UI

* fix: allow scroll in GoPro dialog chat preview

* feat: improve AI usage feedback when theme generation when success or failure

* feat: Message UI enhancement when assistant message is error feedback

* feat: add latest features in /ai page and refactor Core Theme Generation logic hooks

* styles: improve styles and layout for consistency

* styles: improve styles in settings pages for consistency

* cancelled theme doesn't cost requests

* update next to latest (for turbopack compat)

* fail build if polar webhook secret is not set

* update polar sdk and nextjs plugin

* feat: Create customer before creating checkout for them

* chore: save closable chat suggestions state as a preference in local storage

* chore: improve color contrast in user UI message

* feat: add pro features components

* chore: update TextArea styles

* feat: adjust theme generation types, functions and API to support adding an image

* feat: AI components now support and display images

* feat: add support for multiple images

* feat: add support for multiple images to components

* feat: support image only prompts and clean up API code

* add ai-usage schema and actions

* feat: dashboard: add AI usage stats with bar chart

* feat: enforce subscription limits and improved error handling for AI theme generation

* feat: Integrate alert-banner with actual usage

* fix: pricing navigation

* feat: Checkout link generation

* feat: add quick action to create a theme from an image in ai page

* feat: add drag and drop for images

* chore: move Image Upload constant vars and update imports

* feat: improve feedback while theme is being generated

* feat: Add pricing page UI

* refactor: Dashboard migration to Settings page

* feat: improve pricing page

* feat: add auth check in checkout and refactor implementation for consistency and typesafety

* refactor: adjust code and API to follow new types and useQuery hook typesafety

* refactor: alert banner displays correctly and uses the new subscription hook

* feat: improve UI feedback for Pro users

* feat: upload images by pasting (cmd/crtl + v)

* feat: add Go Pro dialog, and global store (no implemented in app yet)

* chore(WIP): scaffold of Pro subscription modal show logic

* feat: Add openProModal logic to handleGenerateTheme in chat-interface

* refactor: follow naming convention

* chore: refactor components and layout/pages for consistency

* fix: enforce auth checks or redirect in /settings routes

* chore: update Go Pro dialog

* refactor: create reusable theme toggle component

* feat: add Get Pro dialog to header

* chore: rename GoPro dialog to GetPro and update imports

* feat: add AI generation section to Home page and adjust Get Pro dialog

* feat: Add customer portal

* feat: add chat input draft persistence (not images)

* styles: improve styles for ai generation and alert banner defaults

* feat: Send whole chat context to AI

* fix: silly typo lol

* feat: support images persistence and sync

* feat: edit message (text, mentions and images)

* fix: add eslint-disable for exhaustive-deps warnings

* chore: fix build error & improve text and styles in suggested prompts

* refactor: handle session and subscription checks and handle post login actions

* migrate to turbopack

* refactor: use reducer builder pattern to sync ai-draft-store with image-uploader

* fix: ai-usage computation: account for abort

* feat: Theme limit handling for pro tier

* refactor: remove duplicate code from ai-chat-form and chat-input

* feat: Add faq to pricing page

* fix: /dashboard navigations -> /settings

* feat: update ui for success page

* feat: Migrate ai chat and draft message store to idb-keyval

* feat: move subscription details in-house

* generate migration

* fix: build err

* styles: improve Message UI

* fix: allow scroll in GoPro dialog chat preview

* feat: improve AI usage feedback when theme generation when success or failure

* feat: Message UI enhancement when assistant message is error feedback

* feat: add latest features in /ai page and refactor Core Theme Generation logic hooks

* styles: improve styles and layout for consistency

* styles: improve styles in settings pages for consistency

* cancelled theme doesn't cost requests

* update next to latest (for turbopack compat)

* fail build if polar webhook secret is not set

* update polar sdk and nextjs plugin

* feat: Create customer before creating checkout for them

---------

Co-authored-by: Luis Llanes <luisllaboj@gmail.com>
2025-07-17 00:00:07 +05:30

37 lines
1015 B
TypeScript

import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";
import { API_AUTH_PREFIX, DEFAULT_LOGIN_REDIRECT } from "./routes";
export async function middleware(request: NextRequest) {
const session = await auth.api.getSession({
headers: await headers(),
});
const pathname = request.nextUrl.pathname;
const isApiAuth = pathname.startsWith(API_AUTH_PREFIX);
if (isApiAuth) {
return NextResponse.next();
}
if (!session) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, request.url));
}
if (session) {
// Redirect logged-in users from /dashboard or /settings (root) to /settings/themes
if (pathname === "/dashboard" || pathname === "/settings") {
return NextResponse.redirect(new URL("/settings/themes", request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/editor/theme/:themeId", "/dashboard", "/settings/:path*", "/success"],
};