forked from mckaywrigley/chatbot-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
82 lines (71 loc) · 2.49 KB
/
Copy pathmiddleware.ts
File metadata and controls
82 lines (71 loc) · 2.49 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
import { i18nRouter } from "next-i18n-router"
import { NextResponse, type NextRequest } from "next/server"
import i18nConfig from "./i18nConfig"
import { createClient } from "@supabase/supabase-js"
export async function middleware(request: NextRequest) {
const i18nResult = i18nRouter(request, i18nConfig)
if (i18nResult) return i18nResult
try {
// Create Supabase client for this request
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
auth: {
persistSession: false
}
}
)
// Get session from cookies - check multiple possible cookie names
const cookieStore = request.cookies
const supabaseSessionCookie =
cookieStore.get('sb-access-token')?.value ||
cookieStore.get('supabase-auth-token')?.value ||
cookieStore.get('sb-refresh-token')?.value
if (supabaseSessionCookie) {
// Set the session cookie for the Supabase client
supabase.auth.setSession({
access_token: supabaseSessionCookie,
refresh_token: ''
})
}
// Log all cookies for debugging
console.log("Available cookies:", [...cookieStore.getAll()].map(c => c.name));
const { data: { session } } = await supabase.auth.getSession()
// If user is authenticated and on the home page, redirect to chat
const redirectToChat = session && request.nextUrl.pathname === "/"
if (redirectToChat) {
try {
const { data: homeWorkspace, error } = await supabase
.from("workspaces")
.select("*")
.eq("user_id", session.user.id)
.eq("is_home", true)
.single()
if (homeWorkspace) {
return NextResponse.redirect(
new URL(`/${homeWorkspace.id}/chat`, request.url)
)
} else {
// If no home workspace exists, try to create one or redirect to login
console.error("No home workspace found for user:", session.user.id);
return NextResponse.redirect(new URL("/login", request.url))
}
} catch (error) {
console.error("Error fetching home workspace:", error)
return NextResponse.redirect(new URL("/login", request.url))
}
}
return NextResponse.next()
} catch (e) {
console.error("Middleware error:", e)
return NextResponse.next({
request: {
headers: request.headers
}
})
}
}
export const config = {
matcher: "/((?!api|static|.*\\..*|_next|auth).*)"
}