Dealing with Supabase Auth

Hey guys! I set up a user schema in drizzle and linked it to the auth users in supabase
export const authSchema = pgSchema('auth')

export const authUsers = authSchema.table('users', {
id: uuid('id').primaryKey().notNull(),
})

export const user = pgTable('user', {
id: uuid('id')
.primaryKey()
.notNull()
.references(() => authUsers.id, { onDelete: 'cascade' }),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at'),
name: text('name').notNull(),
})
export const authSchema = pgSchema('auth')

export const authUsers = authSchema.table('users', {
id: uuid('id').primaryKey().notNull(),
})

export const user = pgTable('user', {
id: uuid('id')
.primaryKey()
.notNull()
.references(() => authUsers.id, { onDelete: 'cascade' }),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at'),
name: text('name').notNull(),
})
I edited the migration files to make sure it doesn't return error since auth exists. But I don't understand how auth workflow is suppossed to work. Currently I can create a user with supabase, what I imagine is that I am supposed to link them somehow so that I can then first create user in supabase then continue to create user in user table. But then how do I deal with sign-in's and other stuff. I need to store much more information about the user. Like for example I need to store certain roles about the user, if I log in with supabase then how am I supposed to access the details from drizzle. Any help would be much appreciated, and thanks in advance!
4 Replies
francis
francis9mo ago
use a trigger to create user table entries on auth table entry creation
Omar Goubail
Omar GoubailOP9mo ago
Hey thank you for your relpy, I did just that and I understand things a lot more thank you. But I have a small issue, I am working with local supabase, and the sql editor does not save queries, furthermore when I link my local project to my live environment will it apply my functions and triggers there too, or do I have to rewrite them there?
francis
francis9mo ago
you need to create your functions and triggers through a supabase migration that you can then apply in production
francis
francis9mo ago
Gist
Create a public.profile table and keep it in sync with supabase `...
Create a public.profile table and keep it in sync with supabase auth.users for selected fields in both directions. - supabase_profile_sync.sql
Want results from more Discord servers?
Add your server