railyard/apps/ritgram/web/middleware.ts
caelum0x ea6437d619 fix(ritgram): RIT-ONB icon-only GitHub connect, dedupe public-path gate
Onboarding welcome screen showed a text "Continue with GitHub" button
even though NEXT_PUBLIC_GITHUB_CLIENT_ID is unset in prod (nothing to
connect to) -- now hidden entirely when unconfigured, icon-only when it
is (Apple-design icon-only-chrome rule). Also hoisted the public-path
allowlist (middleware's auth gate + the API client's 401-redirect) into
one shared apps/ritgram/web/lib/publicPaths.ts so the two definitions
can't drift: the 401 interceptor previously hard-redirected any
cookie-less visitor on a public page (e.g. /explore's best-effort
"recommended for you" call) straight to /login before the page's own
try/catch ever ran. ContentComposer's submit button is icon-only too,
same rule, with an aria-label/title carrying the real action text for
accessibility. tsc --noEmit clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 21:27:14 +03:00

104 lines
3.0 KiB
TypeScript

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import {
buildRepoPath,
buildRepoTextRawPath,
buildRepoTextPath,
isReservedRepoAliasSegment,
} from './utils/repositoryRouting'
import { isPublicPath } from './lib/publicPaths'
// PH launch (2026-11-17): `/explore` is public read-only. Its page component
// (app/explore/page.tsx) has no client-side auth redirect — it already
// renders trending repos for anonymous visitors. `/feed` and `/trending`
// were NOT made public: both hardcode `router.push('/login')` when
// `!isAuthenticated`, so unlike `/explore` that's a real feature change,
// not a small/safe one — left as-is.
//
// The full public-path list now lives in ./lib/publicPaths.ts, shared with
// services/api.ts's 401 interceptor so the two definitions can't drift.
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const segments = pathname.split('/').filter(Boolean)
// Allow public paths
if (isPublicPath(pathname)) {
return NextResponse.next()
}
// Allow static files and API routes
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/api') ||
pathname.includes('.')
) {
return NextResponse.next()
}
if (
segments.length === 5 &&
segments[3] === 'text' &&
segments[4] === 'raw' &&
(segments[0] === 'repo' || segments[0] === 'project' || segments[0] === 'github')
) {
const url = request.nextUrl.clone()
url.pathname = buildRepoTextRawPath(segments[1], segments[2])
return NextResponse.redirect(url)
}
if (
segments.length === 4 &&
segments[3] === 'text' &&
(segments[0] === 'repo' || segments[0] === 'project' || segments[0] === 'github')
) {
const url = request.nextUrl.clone()
url.pathname = buildRepoTextPath(segments[1], segments[2])
return NextResponse.redirect(url)
}
if (
segments.length === 3 &&
(segments[0] === 'repo' || segments[0] === 'project' || segments[0] === 'github')
) {
const destination = buildRepoPath(segments[1], segments[2])
const url = request.nextUrl.clone()
url.pathname = destination
return NextResponse.redirect(url)
}
if (
segments.length === 4 &&
segments[2] === 'text' &&
segments[3] === 'raw' &&
!isReservedRepoAliasSegment(segments[0])
) {
return NextResponse.next()
}
if (
segments.length === 3 &&
segments[2] === 'text' &&
!isReservedRepoAliasSegment(segments[0])
) {
return NextResponse.next()
}
if (segments.length === 2 && !isReservedRepoAliasSegment(segments[0])) {
return NextResponse.next()
}
// For protected routes, check if user has a token cookie
// Note: Gitgram uses localStorage for auth, but we set a lightweight
// cookie indicator on login to enable server-side redirect.
// If no cookie, the client-side auth check in each page will handle redirect.
return NextResponse.next()
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico).*)',
],
}