Back

How to check if the username or the email already exists

  • 1
  • Flutter
  • Accounts
plushteddy
21 Feb, 2024, 15:07

Hello, how can I check if the Email or the username already exists and give seperate error messages?

TL;DR
Developers are trying to check if the email or username already exist by creating a function that triggers after each user change to insert values into a separate collection and then verify uniqueness. Wrap the creation process in a `try/catch` block, catch the error, and display a custom message when error `409` occurs to differentiate between existing email and username.
Binyamin
21 Feb, 2024, 15:09

Hi A. As of now username is not available for Appwrite users. B. You'll need to wrap it with a try/catch block, capture the error by yourself if so for the `create` method, then if you get an error `409` it means the user already exist and you can show a custom error message for the user.

plushteddy
21 Feb, 2024, 15:10

sorry i mean name not username

Binyamin
21 Feb, 2024, 15:11

Name is not unique and two users can have the same name

plushteddy
21 Feb, 2024, 15:11

my problem is just that if i do a try catch it just says that one of them already exists but i dont know which of them

Binyamin
21 Feb, 2024, 15:11

That's good

plushteddy
21 Feb, 2024, 15:11

but i want it to be unique (app side)

plushteddy
21 Feb, 2024, 15:11
TypeScript
try {
                  await account().create(
                    userId: ID.unique(),
                    email: email,
                    password: password,
                    name: _usernameController.text,
                  );
                } catch (error) {
                  final appwriteError = error as AppwriteException;

                  throw Exception(appwriteError.message);
                }```
Binyamin
21 Feb, 2024, 15:11

What you mean one of them, you're creating many in a loop?

plushteddy
21 Feb, 2024, 15:13

theres an input for email and for username (name)

if the name already exists it says this: Error: Exception: A user with the same id, email, or phone already exists in this project. if the email already exists it says this: Error: Exception: A user with the same id, email, or phone already exists in this project.

Binyamin
21 Feb, 2024, 15:13

For this, the best would be to create a function that will run (trigger) after each user change You'll need to create another Collection with the current user name

So the flow can be like so

  1. When the user is setting his name for the first time, you then, insert that value into the usernames collection.
  2. When the user is updating his name, the function checks if that username is taken, if so it will change it to the old one back
plushteddy
21 Feb, 2024, 15:14

ah

Binyamin
21 Feb, 2024, 15:15

The name is not unique So this error can mean only:

  • ID exist
  • Email exist
  • Phone exist

As you don't use the phone you can eliminate the one, For ID to exist is pretty rare, so it means the email exist

plushteddy
21 Feb, 2024, 15:15

I probably try it tomorrow, so i'l probably ask something then

but thanks for your support

Binyamin
21 Feb, 2024, 15:15

:appwritefire:

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more