Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
114 lines (97 loc) · 4.24 KB
/
Copy pathproxy.ts
File metadata and controls
114 lines (97 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { NextRequest, NextResponse } from 'next/server';
import { createServerClient } from '@supabase/ssr';
/**
* DriftGrid request proxy — v1 simplified auth model:
*
* • Local dev (localhost) → no auth required. Ever. Users work offline, free tier.
* • Production → Supabase Auth required for protected routes.
* • Public paths → always accessible (share links, client reviews, API endpoints that need it).
*
* Renamed from `middleware.ts` to `proxy.ts` for Next.js 16 — the middleware
* file convention is deprecated. Exported function is `proxy` to match.
*/
const PUBLIC_PATHS = [
'/_next',
'/favicon.ico',
'/login',
'/forgot-password', // password reset — request a reset email
'/reset-password', // password reset — landing from the email magic link
'/pricing', // pricing page — visible before sign-in
'/api/auth', // Supabase OAuth callback (/api/auth/callback)
'/api/stripe', // Stripe webhook + checkout (routes do their own auth)
'/api/share', // share link creation uses its own auth
'/api/s/', // public share link endpoints
'/api/cloud/', // cloud push/share/verify endpoints (JWT-authed, not cookie-authed)
'/connect', // auth popup page for local→cloud linking
'/s/', // v1 share link pages
'/review/', // legacy client-review pages (live client URLs depend on this)
'/api/manifest/', // public manifest reads for shared projects
'/api/html/', // public HTML file reads for shared projects
];
function isPublicPath(pathname: string): boolean {
return PUBLIC_PATHS.some(p => pathname.startsWith(p));
}
function isLocalDev(request: NextRequest): boolean {
const host = request.nextUrl.hostname;
return host === 'localhost' || host === '127.0.0.1' || host === '0.0.0.0';
}
export async function proxy(request: NextRequest) {
// Next.js 16: this is the renamed middleware. The function name is meaningful.
const { pathname } = request.nextUrl;
// Always allow public paths (share links, login, static assets, etc.)
if (isPublicPath(pathname)) {
return NextResponse.next();
}
// Local dev always bypasses auth — free tier runs fully offline.
if (isLocalDev(request)) {
return NextResponse.next();
}
// Supabase email confirmation sends ?code= to the Site URL (root).
// Redirect to the auth callback to exchange the code for a session.
const code = request.nextUrl.searchParams.get('code');
if (code && !pathname.startsWith('/api/auth')) {
const callbackUrl = new URL('/api/auth/callback', request.url);
callbackUrl.searchParams.set('code', code);
callbackUrl.searchParams.set('next', '/');
return NextResponse.redirect(callbackUrl);
}
// Root landing page — always allow (the page component decides whether to
// show the public landing or the authed dashboard).
if (pathname === '/') {
return NextResponse.next();
}
// Production — require Supabase Auth for everything else.
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseAnonKey) {
// Supabase not configured in a non-local environment — fail closed.
const loginUrl = new URL('/login', request.url);
loginUrl.searchParams.set('error', 'auth_not_configured');
return NextResponse.redirect(loginUrl);
}
let response = NextResponse.next({ request: { headers: request.headers } });
const supabase = createServerClient(supabaseUrl, supabaseAnonKey, {
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
for (const { name, value, options } of cookiesToSet) {
request.cookies.set(name, value);
response = NextResponse.next({ request: { headers: request.headers } });
response.cookies.set(name, value, options);
}
},
},
});
const { data: { user } } = await supabase.auth.getUser();
if (!user) {
const loginUrl = new URL('/login', request.url);
loginUrl.searchParams.set('next', pathname + request.nextUrl.search);
return NextResponse.redirect(loginUrl);
}
return response;
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
};