Is there a way to get auth users based on their third-party login ID?
The Goal
When a role is added to a user in Discord, I'm trying to see if they also have a user in my Supabase Auth instance. If so, I'll end up creating some new roles in the database.
The Problem
I haven't found a way to query SupabaseAuth users based on their third party connections. Ideally, I could use
supabase.auth.admin.getUserById(discordUserID)
(or maybe .getUserByDiscordId(discordUserID)
or .getUserByThirdPartyId(discordUserID)
) to get the user based on their Discord ID, but from digging through the docs and the types that doesn't seem to exist. Am I missing something?
Potential Alternatives
If there's not a built-in solution for this, it seems like my best bet is to create a join table that gets updated whenever a new user is created for matching users to their connection IDs. Some thing like this...
That would allow me to use this table to get the correct user...
10 Replies
It would also work if there was a way to directly query the identities table, like this:
But I get an error when I try to query it, even if I'm using a service role key.
You can't access the auth schema from the API
Also confirmed that accessing
identities
is — at least theoretically — locked, preventing me from creating policies on it. According to the docs service roles shouldn't be affected by policies anyway, but... 🤷🏻♂️
You would need an rpc call to a security definer function.
You are not supposed to write anything to auth schema unless documented somewhere (like app_meta_data or user_meta_data).
For sure. I don't need to write anything to it, but I was investigating policies to see if there was a way I could enable
SELECT
on the table. Clearly not, tho.No the API can not access auth at all.
Yeah, that requires the user to be authenticated, tho. I'm making the request from a secure backend that is using a service role key, not an authenticated user token.
Got it. By "security definer function," do you mean a stored procedure that would handle the query for me? Or something else?
Yes. Security definer means it runs as postgres user (usually) so it can access everything.
Took a few tries to get it figured out, but I just got it all to work! Here's my heavily commented solution in case anybody runs across this issue in the future. I tried to cover all of the things that tripped me up. 😉
And here's the JS I used to call the function via RPC any time a Discord user has a role added or removed.