Back

Issue with Authentication

  • 0
  • Databases
  • Auth
  • Web
lakshika
29 Oct, 2024, 20:41

I am stuck with authentication using appwrite. I am getting bad request 400 error when trying to register though we already have project, database, collection created on Appwrite and those are correctly configured in our application code as well.

TL;DR
Issue: Error 400 (Bad Request) when creating a new document for user registration. Solution: - Double-check permissions on the collection. - Review the create account code in auth.js for any potential errors. - Ensure project, database, and collection are correctly configured in the application code.
Kenny
29 Oct, 2024, 20:43

Can you post the relevant code, and the exact error being thrown?

lakshika
29 Oct, 2024, 20:47

This is the code for creating account in auth.js file

async createAccount({email, password, name, address, city, role}) { try { const userAccount = await this.account.create(ID.unique(), email, password); if(userAccount) { //call another method

TypeScript
            await this.database.createDocument(
                conf.appWriteDatabaseId,
                conf.appWriteCollectionId,
                ID.unique(),
                {
                    userId: userAccount.$id, // Link this document to the created user
                    name, 
                    address,
                    city,
                    role
                }
            );

            console.log("User document created:", document); // Log created document
            return this.login({email, password});
        }
        else {
            return userAccount;
        }
    } catch (error) {
        console.error("Error creating account:", error);
        throw error;
    }
}

The error I am getting in console is: POST https://cloud.appwrite.io/v1/databases/671fbea…/collections/671fc12…/documents 400 (Bad Request) createAccount @ auth.js:23 await in createAccount
handleSubmit @ Register.js:154 Show 23 more frames

Kenny
29 Oct, 2024, 20:49

So it looks like it's throwing an error when trying to create a new document. Are permissions setup correctly on that collection?

lakshika
29 Oct, 2024, 20:51

Yes, my collection has all 4 CRUD operations access for all users

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