Skip to content

Commit a939e92

Browse files
committed
Feat: Connecting Supabase
1 parent 149bd32 commit a939e92

File tree

5 files changed

+246
-8
lines changed

5 files changed

+246
-8
lines changed

package-lock.json

Lines changed: 155 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
"lint": "eslint"
1010
},
1111
"dependencies": {
12+
"@supabase/ssr": "^0.7.0",
13+
"@supabase/supabase-js": "^2.75.0",
14+
"next": "15.5.3",
1215
"react": "19.1.0",
13-
"react-dom": "19.1.0",
14-
"next": "15.5.3"
16+
"react-dom": "19.1.0"
1517
},
1618
"devDependencies": {
17-
"typescript": "^5",
19+
"@eslint/eslintrc": "^3",
20+
"@tailwindcss/postcss": "^4",
1821
"@types/node": "^20",
1922
"@types/react": "^19",
2023
"@types/react-dom": "^19",
21-
"@tailwindcss/postcss": "^4",
22-
"tailwindcss": "^4",
2324
"eslint": "^9",
2425
"eslint-config-next": "15.5.3",
25-
"@eslint/eslintrc": "^3"
26+
"tailwindcss": "^4",
27+
"typescript": "^5"
2628
}
2729
}

src/lib/supabase/client.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createBrowserClient } from '@supabase/ssr'
2+
3+
export function createClient() {
4+
return createBrowserClient(
5+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
6+
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!
7+
)
8+
}

src/lib/supabase/middleware.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { createServerClient } from '@supabase/ssr'
2+
import { NextResponse, type NextRequest } from 'next/server'
3+
4+
export async function updateSession(request: NextRequest) {
5+
let supabaseResponse = NextResponse.next({
6+
request,
7+
})
8+
9+
const supabase = createServerClient(
10+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
11+
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
12+
{
13+
cookies: {
14+
getAll() {
15+
return request.cookies.getAll()
16+
},
17+
setAll(cookiesToSet) {
18+
cookiesToSet.forEach(({ name, value, options }) => request.cookies.set(name, value))
19+
supabaseResponse = NextResponse.next({
20+
request,
21+
})
22+
cookiesToSet.forEach(({ name, value, options }) =>
23+
supabaseResponse.cookies.set(name, value, options)
24+
)
25+
},
26+
},
27+
}
28+
)
29+
30+
const {
31+
data: { user },
32+
} = await supabase.auth.getUser()
33+
34+
if (
35+
!user &&
36+
!request.nextUrl.pathname.startsWith('/login') &&
37+
!request.nextUrl.pathname.startsWith('/error')
38+
) {
39+
// no user, potentially respond by redirecting the user to the login page
40+
const url = request.nextUrl.clone()
41+
url.pathname = '/login'
42+
return NextResponse.redirect(url)
43+
}
44+
45+
return supabaseResponse
46+
}

src/lib/supabase/server.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createServerClient } from '@supabase/ssr'
2+
import { cookies } from 'next/headers'
3+
4+
export async function createClient() {
5+
const cookieStore = await cookies()
6+
7+
return createServerClient(
8+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
9+
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
10+
{
11+
cookies: {
12+
getAll() {
13+
return cookieStore.getAll()
14+
},
15+
setAll(cookiesToSet) {
16+
try {
17+
cookiesToSet.forEach(({ name, value, options }) =>
18+
cookieStore.set(name, value, options)
19+
)
20+
} catch {
21+
// The `setAll` method was called from a Server Component.
22+
// This can be ignored if you have middleware refreshing
23+
// user sessions.
24+
}
25+
},
26+
},
27+
}
28+
)
29+
}

0 commit comments

Comments
 (0)