Unverified Commit 8659d47c authored by Stojan Dimitrovski's avatar Stojan Dimitrovski Committed by GitHub

chore: Update `with-supabase` example to use `getClaims()` (#81383)

Updates the `with-supabase` example to use the new and much faster (when
project is configured with asymmetric JWT support)
`supabase.auth.getClaims()` function instead of `getUser()`.

---------
Co-authored-by: 's avatarBenjamin Woodruff <benjamin.woodruff@vercel.com>
parent d0ea7129
# Update these with your Supabase details from your project settings > API # Update these with your Supabase details from your project settings > API
# https://app.supabase.com/project/_/settings/api # https://app.supabase.com/project/_/settings/api
NEXT_PUBLIC_SUPABASE_URL=your-project-url NEXT_PUBLIC_SUPABASE_URL=your-project-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key NEXT_PUBLIC_SUPABASE_PUBLISHABLE_OR_ANON_KEY=your-anon-key
...@@ -7,8 +7,8 @@ import { FetchDataSteps } from "@/components/tutorial/fetch-data-steps"; ...@@ -7,8 +7,8 @@ import { FetchDataSteps } from "@/components/tutorial/fetch-data-steps";
export default async function ProtectedPage() { export default async function ProtectedPage() {
const supabase = await createClient(); const supabase = await createClient();
const { data, error } = await supabase.auth.getUser(); const { data, error } = await supabase.auth.getClaims();
if (error || !data?.user) { if (error || !data?.claims) {
redirect("/auth/login"); redirect("/auth/login");
} }
...@@ -24,7 +24,7 @@ export default async function ProtectedPage() { ...@@ -24,7 +24,7 @@ export default async function ProtectedPage() {
<div className="flex flex-col gap-2 items-start"> <div className="flex flex-col gap-2 items-start">
<h2 className="font-bold text-2xl mb-4">Your user details</h2> <h2 className="font-bold text-2xl mb-4">Your user details</h2>
<pre className="text-xs font-mono p-3 rounded border max-h-32 overflow-auto"> <pre className="text-xs font-mono p-3 rounded border max-h-32 overflow-auto">
{JSON.stringify(data.user, null, 2)} {JSON.stringify(data.claims, null, 2)}
</pre> </pre>
</div> </div>
<div> <div>
......
...@@ -6,9 +6,10 @@ import { LogoutButton } from "./logout-button"; ...@@ -6,9 +6,10 @@ import { LogoutButton } from "./logout-button";
export async function AuthButton() { export async function AuthButton() {
const supabase = await createClient(); const supabase = await createClient();
const { // You can also use getUser() which will be slower.
data: { user }, const { data } = await supabase.auth.getClaims();
} = await supabase.auth.getUser();
const user = data?.claims;
return user ? ( return user ? (
<div className="flex items-center gap-4"> <div className="flex items-center gap-4">
......
...@@ -3,6 +3,6 @@ import { createBrowserClient } from "@supabase/ssr"; ...@@ -3,6 +3,6 @@ import { createBrowserClient } from "@supabase/ssr";
export function createClient() { export function createClient() {
return createBrowserClient( return createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_OR_ANON_KEY!,
); );
} }
...@@ -7,14 +7,17 @@ export async function updateSession(request: NextRequest) { ...@@ -7,14 +7,17 @@ export async function updateSession(request: NextRequest) {
request, request,
}); });
// If the env vars are not set, skip middleware check. You can remove this once you setup the project. // If the env vars are not set, skip middleware check. You can remove this
// once you setup the project.
if (!hasEnvVars) { if (!hasEnvVars) {
return supabaseResponse; return supabaseResponse;
} }
// With Fluid compute, don't put this client in a global environment
// variable. Always create a new one on each request.
const supabase = createServerClient( const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_OR_ANON_KEY!,
{ {
cookies: { cookies: {
getAll() { getAll() {
...@@ -36,14 +39,13 @@ export async function updateSession(request: NextRequest) { ...@@ -36,14 +39,13 @@ export async function updateSession(request: NextRequest) {
); );
// Do not run code between createServerClient and // Do not run code between createServerClient and
// supabase.auth.getUser(). A simple mistake could make it very hard to debug // supabase.auth.getClaims(). A simple mistake could make it very hard to debug
// issues with users being randomly logged out. // issues with users being randomly logged out.
// IMPORTANT: DO NOT REMOVE auth.getUser() // IMPORTANT: If you remove getClaims() and you use server-side rendering
// with the Supabase client, your users may be randomly logged out.
const { const { data } = await supabase.auth.getClaims();
data: { user }, const user = data?.claims;
} = await supabase.auth.getUser();
if ( if (
request.nextUrl.pathname !== "/" && request.nextUrl.pathname !== "/" &&
......
import { createServerClient } from "@supabase/ssr"; import { createServerClient } from "@supabase/ssr";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
/**
* Especially important if using Fluid compute: Don't put this client in a
* global variable. Always create a new client within each function when using
* it.
*/
export async function createClient() { export async function createClient() {
const cookieStore = await cookies(); const cookieStore = await cookies();
return createServerClient( return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_OR_ANON_KEY!,
{ {
cookies: { cookies: {
getAll() { getAll() {
......
...@@ -8,4 +8,4 @@ export function cn(...inputs: ClassValue[]) { ...@@ -8,4 +8,4 @@ export function cn(...inputs: ClassValue[]) {
// This check can be removed, it is just for tutorial purposes // This check can be removed, it is just for tutorial purposes
export const hasEnvVars = export const hasEnvVars =
process.env.NEXT_PUBLIC_SUPABASE_URL && process.env.NEXT_PUBLIC_SUPABASE_URL &&
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_OR_ANON_KEY;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment