6

How can I check if a user with the same email already exists when Enable email confirmations is turned on.

When Enable email confirmations are turned on an obfuscated / fake user object is returned by supabase, according to this comment it's a feature.

How can I check if the email already exists without turning off Email confirmation?

I tried passing using supabase.auth.api.getUser, but that needs JWT, not the user id.

1
  • 1
    interesting choice but according to github.com/supabase/supabase-js/issues/… you shouldn't need to check since the user is redirected via mail back to your application Commented Sep 21, 2022 at 16:42

3 Answers 3

12

Not sure if following is the correct way but I do it as follows (TypeScript):

const [errorMsg, setErrorMsg] = useState<string | null>(null);
const [successMsg, setSuccessMsg] = useState<string | null>(null);

async function signUp(formData: Values) {
  const { data, error } = await supabase.auth.signUp({
    email: formData.email,
    password: formData.password,
  });

  if (error) {
    setErrorMsg(error.message);
  } else if (data.user?.identities?.length === 0) {
    setErrorMsg('User already registered');
  } else {
    setSuccessMsg('Success! Please check your inbox.');
  }
}

data.user?.identities?.length is 0 when the user is registered already.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't fully understand why identities would be empty if a user is registered so I thought I'd add to this answer: If signUp() is called for an existing confirmed user: When both Confirm email and Confirm phone (even when phone provider is disabled) are enabled in your project, an obfuscated/fake user object is returned. Source: supabase.com/docs/reference/javascript/auth-signup
Note: this works only for accounts with confirmed emails. You can add an extra check !data.user?.confirmed_at to check for if an account exists that has not yet confirmed their email.
1

Just go to providers and disable phone confirmation even if phone provider is disabled. Then you will get it in errors field. That user_already_exists

https://supabase.com/docs/reference/javascript/auth-signup

Comments

1

Going to the providers section and disabling phone confirmation fixes the issue, make sure to disable the phone confirmation and not just the phone provider, doing these will result in supabase finally giving the user already registered error.

Disabling The Phone Confirmation Under Providers (Phone)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.