Supabase: How to fetch a user by email?

Self explanatory from the title. We have const { data: { user }, error: authError } = await supabase.auth.admin.getUserById('5d16dc00-4d1d-4026-a60a-ccf5b549da15'); Is there an equivalent for fetching the user through their email address? Thank you.
5 Replies
garyaustin
garyaustin2w ago
No. You could write your own Postgres function to return from auth.users a select on email column and call with RPC. Just make sure to secure it by removing grants from anon and authenticated or check for service_role in auth.jwt()->>'role'.
Noroup
NoroupOP2w ago
I will give this a go, thank you
{
code: 'PGRST202',
details: 'Searched for the function public.public.fetchuser_email with parameter var_emailAddr or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.',
hint: 'Perhaps you meant to call the function public.fetchuser_email',
message: 'Could not find the function public.public.fetchuser_email(var_emailAddr) in the schema cache'
}
{
code: 'PGRST202',
details: 'Searched for the function public.public.fetchuser_email with parameter var_emailAddr or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.',
hint: 'Perhaps you meant to call the function public.fetchuser_email',
message: 'Could not find the function public.public.fetchuser_email(var_emailAddr) in the schema cache'
}
Hmm
Noroup
NoroupOP2w ago
No description
Noroup
NoroupOP2w ago
Nvm, permission denied for table users Am I supposed to add the service_role for the user's table? Solved by running:
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE auth.users TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE auth.users TO service_role;
garyaustin
garyaustin2w ago
The better way was to make the function security definer type. But that way works too.

Did you find this page helpful?