-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d831834
commit f933671
Showing
6 changed files
with
130 additions
and
30 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createBrowserClient } from '@supabase/ssr'; | ||
|
||
export function createSupabaseBrowserClient() { | ||
return createBrowserClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { type NextRequest, type NextResponse } from "next/server"; | ||
import { cookies } from "next/headers"; | ||
import { getCookie, setCookie } from "cookies-next"; | ||
import { createServerClient, type CookieOptions } from "@supabase/ssr"; | ||
|
||
// server component can only get cookies and not set them, hence the "component" check | ||
export function createSupabaseServerClient(component: boolean = false) { | ||
return createServerClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, | ||
{ | ||
cookies: { | ||
get(name: string) { | ||
return cookies().get(name)?.value; | ||
}, | ||
set(name: string, value: string, options: CookieOptions) { | ||
if (component) return; | ||
cookies().set(name, value, options); | ||
}, | ||
remove(name: string, options: CookieOptions) { | ||
if (component) return; | ||
cookies().set(name, "", options); | ||
}, | ||
}, | ||
} | ||
); | ||
} | ||
|
||
export function createSupabaseServerComponentClient() { | ||
return createSupabaseServerClient(true); | ||
} | ||
|
||
export function createSupabaseReqResClient( | ||
req: NextRequest, | ||
res: NextResponse | ||
) { | ||
return createServerClient( | ||
process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, | ||
{ | ||
cookies: { | ||
get(name: string) { | ||
return getCookie(name, { req, res }); | ||
}, | ||
set(name: string, value: string, options: CookieOptions) { | ||
setCookie(name, value, { req, res, ...options }); | ||
}, | ||
remove(name: string, options: CookieOptions) { | ||
setCookie(name, "", { req, res, ...options }); | ||
}, | ||
}, | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { createSupabaseBrowserClient } from "./browser-client"; | ||
import { Session } from "@supabase/supabase-js"; | ||
|
||
export default function useSession() { | ||
const [session, setSession] = useState<Session | null>(null); | ||
|
||
useEffect(() => { | ||
const supabase = createSupabaseBrowserClient(); | ||
|
||
const getSession = async () => { | ||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession(); | ||
|
||
setSession(session); | ||
}; | ||
|
||
getSession(); | ||
}, []); | ||
|
||
return session; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.