question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js 13 `app` directory support

See original GitHub issue

With version v0.5.2 we’ve added a createServerComponentSupabaseClient which can be used server components to get an authenticated Supabase client. This is the PR which includes an example. Note that this needs to be used in combination with middleware as Next.js does not yet allow setting headers in server components.

Docs to follow soon.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:44 (8 by maintainers)

github_iconTop GitHub Comments

18reactions
thorwebdevcommented, Nov 15, 2022

With version v0.5.2 we’ve added a createServerComponentSupabaseClient which can be used server components to get an authenticated Supabase client. This is the PR which includes an example. Note that this needs to be used in combination with middleware as Next.js does not yet allow setting headers in server components.

Docs to follow soon.

10reactions
jensencommented, Nov 15, 2022

I spent some time yesterday and a bit today. Was able to use the existing withMiddlewareAuth function directly.

import { withMiddlewareAuth } from "@supabase/auth-helpers-nextjs";

export const middleware = withMiddlewareAuth({
  redirectTo: "/login",
});

export const config = {
  matcher: "/dashboard/:path*",
};

In order to get access to a supabase client that is authenticated on the server, I am doing something like this:

import { cookies } from "next/headers";
import { createClient } from "@supabase/supabase-js";

export const createSupabaseClient = () => {
  if (!process.env.NEXT_PUBLIC_SUPABASE_URL) {
    throw new Error(`Must define process.env.NEXT_PUBLIC_SUPABASE_URL`);
  }

  if (!process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
    throw new Error(`Must define process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY`);
  }

  const cookie = JSON.parse(cookies().get("supabase-auth-token") || "{}");

  const authorization = cookie?.access_token
    ? {
        global: {
          headers: {
            Authorization: `Bearer ${cookie.access_token}`,
          },
        },
      }
    : {};

  return createClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
    {
      auth: {
        persistSession: false,
      },
      ...authorization,
    }
  );
};

Which I’m calling in my server component.

import { createSupabaseClient } from "services/supabase";

const getCustomers = async () => {
  const supabase = createSupabaseClient();

  return (await supabase.from("customers").select()).data;
};

const Customers = async () => {
  const customers = await getCustomers();

  return <pre>{JSON.stringify(customers, null, 2)}</pre>;
};

export default Customers;

I don’t expect this to be a long term solution, and it doesn’t have the ability to set the cookies yet, but for authenticated requests I don’t need to at this point. Just wanted to give a quick progress update.

The Next 12 repo I’m working on is here: https://github.com/jensen/supabase-nextjs. I will push the Next 13 upgrade when I do some more testing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js 13: Working with the new app directory - LogRocket Blog
Explore the new features introduced in the Next.js 13 app directory ... The new app directory works alongside the pages directory to support...
Read more >
App Directory Roadmap - Next.js beta docs
Section Feature Supported React 18 Server Components ✓ Default React 18 Client Components ✓ Opt‑in React 18 Shared Components 🏗️
Read more >
How the App Directory Works in Next.js 13 - MakeUseOf
Next.js 13 introduced a new routing system using the app directory. ... the app directory supports are server components and streaming.
Read more >
Next.js 13 App Playground - Vercel
Explore the new app directory in Next.js 13. ... Suspense for Data Fetching: async / await support and the use hook for component-level...
Read more >
[Feedback] App Directory in Next.js 13 #41745 - GitHub
However, upgrading to Next.js 13 does not require using the app directory. This discussion is where you can provide feedback on the new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found