Check if the user exists in Auth #1282
-
Is there a way to check if a user exists in Auth by checking the email address? |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 43 replies
-
If you try to create an account with the same email you will be notified that the email is already in use. You could also create a stored procedure to copy the |
Beta Was this translation helpful? Give feedback.
-
I'm using |
Beta Was this translation helpful? Give feedback.
-
I have a work around:
|
Beta Was this translation helpful? Give feedback.
-
In the response there's So we should be able to do something like: if (user.identities.length) {
// please confirm your email, or send again
} else {
// already signed up, sign in instead?
} I'd kinda like supabase to do this for us though and return an error. |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issue here, I would like to query the The reason I would like to do this is that I don't think I guess the solutions at the moment are:
|
Beta Was this translation helpful? Give feedback.
-
This is what I do. I created the function: create or replace function get_user_id_by_email(user_email text) returns uuid
as $$
declare
user_id uuid;
begin
select id
from auth.users
where email = user_email
into user_id;
return user_id;
end;
$$ language plpgsql security invoker; then I revoked all roles' privileges except for service_role using the following query: do
$$
declare
pg_role record;
begin
for pg_role in select rolname from pg_roles
loop
execute 'revoke all on function "public"."get_user_id_by_email" from ' || quote_ident(pg_role.rolname);
end loop;
grant EXECUTE ON function "public"."get_user_id_by_email" to service_role;
end;
$$ finally, I call it on the server with a supabase admin client (created with service role key) to call the function, like so: const {data, error} = await supabase.rpc('get_user_id_by_email', {user_email: '[email protected]'}); if the user exists, you'll get an id, otherwise, the user does not exist. P.S. I noticed that the "supabase_admin" role is also is able to bypass and invoke the function after revoking it from all roles except for service_role. I tested it on anon and authenticated, and they're both unable to invoke it as expected. I figured since supabase_admin is supposed to be granted only for admins, there's no harm. To check the privileges for a specific function, i used the following query: SELECT proacl,proname FROM pg_proc WHERE proname = 'get_user_id_by_email'; and as expected, it shows that the only role who has permission to invoke it is indeed just the service_role; I'm still trying to figure things out, but so far, this is what I have. I hope it helps. |
Beta Was this translation helpful? Give feedback.
-
OK,it's better to grant least permission.
many thanks for your help
…On Sun, Mar 12, 2023 at 20:37 ofeenee ***@***.***> wrote:
@halofe <https://github.com/halofe> I'd like to add one more note if I
may:
the grant ALL ON auth.users to service_role; is a bit "too much" to get
the function to work. The function requires "read access" only, and "grant
ALL" gives service_role the permission to do all operations "select insert
update delete". It's a good "safety switch" to have to prevent accidental
data mutation.
I personally use my service_role client as admin to do certain operations
on auth.users, that's why I grant all. However, if all you need is the
necessary permissions to allow service_role to invoke and use the
function, the following should be enough:
grant select ON auth.users to service_role;
My bad, as I should've mentioned that earlier; I apologize for that.
—
Reply to this email directly, view it on GitHub
<#1282 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A2TS3SS75WRVZMN6PAESYZLW3W7OZANCNFSM43S7LFZQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I started using supabase recently, and I realized I could leverage the identities array inside the Example code:
|
Beta Was this translation helpful? Give feedback.
-
Hi there! After some testing, I've realized that new users do not have the properties |
Beta Was this translation helpful? Give feedback.
-
hey, |
Beta Was this translation helpful? Give feedback.
-
What should we do if the account is pending email verification? Currently, by default, it just sends the confirmation email again and again. Anyone got a clue? |
Beta Was this translation helpful? Give feedback.
If you try to create an account with the same email you will be notified that the email is already in use. You could also create a stored procedure to copy the
auth.user
email into your ownusers
table and you would be able to query that easily. You could probably also create a stored procedure to check if the email already exists in theauth.user
table.