integrating with postgres rls (supabase)

hi. i'm trying to migrate off of supabase auth so i wanted to go with better-auth. my question is, how could i correctly use the JWT integration to setup RLS auth? do i need to always fetch the endpoint to get the JWT token, or could i get it auto-provided every time? that would be much simpler for setup i need to setup RLS for maybe two days with supabase and then i'm migrating to neon (want auth and db to be staggered, don't want a big screwup to hit all at once)
7 Replies
bekacru
bekacru2w ago
@daveycodez might help but check the jwks plugin https://www.better-auth.com/docs/plugins/jwt
JWT | Better Auth
Authenticate users with JWT tokens in services that can't use the session
oof2win2
oof2win2OP2w ago
yeah i saw that plugin + the usage davey has (https://discord.com/channels/1288403910284935179/1296058482289676320/1337526387967266847), just wondering how to set it up what complicates it more i feel is me migrating auth and then db - it makes sense to do like this but makes some parts more annoying / painful
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
oof2win2
oof2win2OP2w ago
hell yeah, thank you hey @daveycodez, do you by chance have any knowledge on how to get supabase working with better-auth? i've looked at your package, but i have no idea how to get supabase to cooperate with the better-auth tokens - do you know how by chance? i have this so far
import { Database } from "@repo/types/supabase";
import { createBrowserClient } from "@supabase/ssr";
import { useToken } from "./use-auth-hooks";

export function useSupabase() {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;

const { token } = useToken();

if (!supabaseUrl || !supabaseAnonKey) {
throw new Error("Missing Supabase environment variables");
}

const supabase = createBrowserClient<Database>(supabaseUrl, supabaseAnonKey, {
accessToken: token || "",
});

return supabase;
}
import { Database } from "@repo/types/supabase";
import { createBrowserClient } from "@supabase/ssr";
import { useToken } from "./use-auth-hooks";

export function useSupabase() {
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;

const { token } = useToken();

if (!supabaseUrl || !supabaseAnonKey) {
throw new Error("Missing Supabase environment variables");
}

const supabase = createBrowserClient<Database>(supabaseUrl, supabaseAnonKey, {
accessToken: token || "",
});

return supabase;
}
turns out i just had to forge my auth token on an api route and put some cache headers on it, kinda sick tbh
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
oof2win2
oof2win2OP2w ago
yeah i know that's the next step - just need to setup proper auth so that i can migrate off supabase part by part @daveycodez i've been looking more and more into this, saw the issue here and convos in discord. how would you ideally have the jwt support work? like it's just stored in the session or something so it can be used on both the client and server in the ideal case, and can be revalidated every 5m or something
Unknown User
Unknown User7d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?