How to reference supabase auth user?

I have supabase authentication login setup, so when user logs into web app, it saves to supabase's authentication table. how do i create a reference from drizzle's schema to the auth UID? i want my project table to reference the logged in user for example:
export const project = pgTable("project", {
id: uuid("id").primaryKey().notNull(),
name: text("name").notNull(),
url: text("url").notNull(),
description: text("description"),
submittedBy: uuid("id").notNull().references(() => auth.users.id, { onDelete: "cascade" }),
createdAt: timestamp("created_at", {
withTimezone: true,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
withTimezone: true,
})
.notNull()
.defaultNow(),
});
export const project = pgTable("project", {
id: uuid("id").primaryKey().notNull(),
name: text("name").notNull(),
url: text("url").notNull(),
description: text("description"),
submittedBy: uuid("id").notNull().references(() => auth.users.id, { onDelete: "cascade" }),
createdAt: timestamp("created_at", {
withTimezone: true,
})
.notNull()
.defaultNow(),
updatedAt: timestamp("updated_at", {
withTimezone: true,
})
.notNull()
.defaultNow(),
});
I saw something similar in another post with the references, but how do i get auth? not sure if this is the correct way still?
8 Replies
Mykhailo
Mykhailo•9mo ago
Hello, @readitron! I am not much familiar with Supabase, but I think you can find solution here: https://discord.com/channels/1043890932593987624/1211924508840173588
readitron
readitronOP•9mo ago
hi @solo thanks for the reply so from this image, this portion:
export const authUsers = authSchema.table("users", {
id: uuid("id").primaryKey().notNull(),
})
export const authUsers = authSchema.table("users", {
id: uuid("id").primaryKey().notNull(),
})
doesn't this mean that its creating a new table? But what if supabase already has a table which is what I'm confused. Because I wanted to reference an existing table that supabase already has.
No description
Mykhailo
Mykhailo•9mo ago
I think it's ok, because in migration you have CREATE TABLE IF NOT EXISTS
loup
loup•9mo ago
I wanna add Drizzle to my NextJS using Supabase, do u know if we can also add RLS in drizzle ?
Mykhailo
Mykhailo•9mo ago
GitHub
Feat: [Pg] support for row level security by Angelelz · Pull Reques...
This PR will close #594. The changes in this PR will not do anything on their own, as this is mostly a migration/push feature from drizzle-kit. Upon merging this, the user will be able to define ro...
loup
loup•9mo ago
but Im quite confusing with Drizzle. For example in supabase, for making RLS we can check the auth() connected for checking if he got right to access. But is it a custom function made by Supabase ? Or Drizzle can know which user is connected for making the righ RLS (like with NextAuth) ? Sry if isnt clear. Im searching a way to be able in a futur to move away from Supaabse without a lot of changes
Mykhailo
Mykhailo•9mo ago
Honestly, I am not familiar with Supabase and NextAuth, so I don't know solution for this, unfortunately 😦
loup
loup•9mo ago
I currently have a Supabase database with lot of function, trigger, and RLS. How did u add Drizzle with schema without overwritting your RLS, etc... I dont wanna loose all my work if Drizzle overwrite my setup
Want results from more Discord servers?
Add your server