Next.js Discord

Discord Forum

Help for setting up auth with Supabase

Unanswered
Woodwasp posted this in #help-forum
Open in Discord
WoodwaspOP
Hey everyone!

i'm trying to setting up a Auth with supabase

I've followed this tuto : https://supabase.com/docs/guides/auth/server-side/nextjs

I'm using App Router

while trying to create my server.js file and access to private page named : addPost (path is /dashboard/blog/addPost)
it say :
× You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/

and when i'm trying to add a "use server" at the top of my server.js file

it say my function must be async, and when I switch my function to async :


import { createServerClient } from "@supabase/ssr"
import { cookies } from "next/headers"

export async function createClient() {
    const cookieStore = cookies()

    return createServerClient(process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KE, {
        cookies: {
            get(name) {
                return cookieStore.get(name)?.value
            },
            set(name, value, options) {
                try {
                    cookieStore.set({ name, value, ...options })
                } catch (error) {}
            },
            remove(name, options) {
                try {
                    cookieStore.set({ name, value: "", ...options })
                } catch (error) {}
            },
        },
    })
}

Nothing working anymore..

It say :
    const { data, error } = await supabase.auth.getUser()
    if (error || !data?.user) {
        redirect("/login")
    }

getUser is undefined


## What I wanted to do at the beggining :

Protect all my dashboard from the DashboardLayout, to not add a function on each page..
If any page have the dashboardlayout, she's automatically protected.. for example my login.jsx doesn't have the dashboard layout.. not necessary to be protected..


Thank for your help 🙏

1 Reply

Madeiran sardinella
Hi, I think you can to use Middleware for that.