Middleware with supabase
Unanswered
Bordin posted this in #help-forum
BordinOP
Hello, as the title says i am trying to create a resuable query using Supabase and NextJS. I am coming on it from an OOP angle, creating a service class which will include all my queries related to a specific table.
However I am having some difficulties with Supabase connection and middleware.
I was following a youtube tutorial where the youtuber created the following middleware code:
From my understanding, the above code creates a connection between the database and the client in every HTTP request. My issue is with resuing this middle ware. I am not sure how
This is the service class I have created:
I am not sure what to replace supabase with. I dont want to create a new client since that should be the middleware job that was created in the youtube tutorial, right?
However I am having some difficulties with Supabase connection and middleware.
I was following a youtube tutorial where the youtuber created the following middleware code:
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
export async function middleware(req) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
await supabase.auth.getSession();
return res;
}From my understanding, the above code creates a connection between the database and the client in every HTTP request. My issue is with resuing this middle ware. I am not sure how
This is the service class I have created:
export default class CategoryReposiroty{
async function GetCategories(){
let { data: Device, error } = await supabase
.from('Device')
.select('*')
}
}I am not sure what to replace supabase with. I dont want to create a new client since that should be the middleware job that was created in the youtube tutorial, right?
9 Replies
Barbary Lion
I suggest moving away from supabase auth helpers, they are deprecated, use the newer ssr supabase approach
@Bordin Hello, as the title says i am trying to create a resuable query using Supabase and NextJS. I am coming on it from an OOP angle, creating a service class which will include all my queries related to a specific table.
However I am having some difficulties with Supabase connection and middleware.
I was following a youtube tutorial where the youtuber created the following middleware code:
js
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs";
import { NextResponse } from "next/server";
export async function middleware(req) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
await supabase.auth.getSession();
return res;
}
From my understanding, the above code creates a connection between the database and the client in every HTTP request. My issue is with resuing this middle ware. I am not sure how
This is the service class I have created:
ts
export default class CategoryReposiroty{
async function GetCategories(){
let { data: Device, error } = await supabase
.from('Device')
.select('*')
}
}
I am not sure what to replace supabase with. I dont want to create a new client since that should be the middleware job that was created in the youtube tutorial, right?
Orinoco Crocodile
as @Barbary Lion said, please use the @supabase/ssr package, a detailed guide is here: https://supabase.com/docs/guides/auth/server-side/creating-a-client
Follow this guide to setup your project with this package:
https://supabase.com/docs/guides/auth/server-side/nextjs
https://supabase.com/docs/guides/auth/server-side/nextjs
as you will see for the App router is just to add three new files: client.ts, server.ts and a utils new middleware.ts file with the Supabase config, is very easy to follow. I hope it helps
BordinOP
i dont understand
i dont understand the middleware role
BordinOP
i followed the document step by step and its not working
whenever i signin it takes me to page /error.tsx