How can I retrieve a user’s ID from their email in Supabase to share documents (like Google Docs)? #40431
-
|
Hey everyone! 👋 I’m using Supabase for authentication and database management, but I can’t seem to find the right way to fetch a user’s ID using their email. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi Kirti, Yeah, I ran into the same issue when I was building a similar project. Here’s how you can do it 👇 Option 1: Using the Supabase Admin API (server-side only) import { createClient } from '@supabase/supabase-js'
const supabaseAdmin = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY // Important: Use Service Role key here
)
async function getUserIdByEmail(email) {
const { data, error } = await supabaseAdmin.auth.admin.getUserByEmail(email)
if (error) {
console.error('Error fetching user:', error.message)
return null
}
return data.user?.id || null
}Then you can use that Option 2: If you store user profiles in a custom table (recommended) const { data, error } = await supabase
.from('profiles')
.select('id')
.eq('email', recipientEmail)
.single()This way, you don’t need to expose your service role key. TL;DR:
That’s the cleanest and safest way to handle sharing docs by email. |
Beta Was this translation helpful? Give feedback.
Hi Kirti,
Yeah, I ran into the same issue when I was building a similar project.
If you’re using Supabase Auth, you can’t directly query the
auth.userstable from the client-side for security reasons. But you can do it safely using a Supabase server-side function (like with Supabase Admin API).Here’s how you can do it 👇
Option 1: Using the Supabase Admin API (server-side only)