Back

Handling user login errors

  • 0
  • Accounts
  • Web
larkx
19 Jul, 2023, 00:44

I'm using Appwrite to create an email session with users. At the moment, I do very basic checks within the login form component that check if the password is longer than 8 characters. I then call a function in a separate utility file that handles the login. How can I check for errors such as no user found? Is there perhaps a boilerplate for an appwrite login and register function that I can look at? I'm fairly new to React, is code within a component secure, i.e: can I call the createEmailSession from the component file?

TL;DR
The user is asking for help regarding handling user login errors. They provided a code example using try catch to handle errors when creating an email session. They are using Appwrite for user authentication. They also asked if there is a boilerplate for an Appwrite login and register function. Finally, they asked if it is secure to call `createEmailSession` from the component file in React. Solution: The user can follow the provided code example to handle errors when creating an email session. As for a boilerplate, they can check Appwrite's documentation or community resources for a sample login and register function. Calling `createEmailSession` from
Drake
19 Jul, 2023, 03:26

You can put it in the component. It's up to you how you want to organize your code.

To handle errors, wrap in try/catch, and then handle the exception

joeyouss
19 Jul, 2023, 09:59

Hi @larkx , here is a pseudo example using try catch:

TypeScript
import { sdk } from 'appwrite'; 
const loginUser = async (email, password) => {
  try {
    await sdk.account.createEmailSession(email, password);
  } catch (error) {
    if (error.code === 'user-not-found' // any other error you are checking for) {
      console.log('User not found. Please check your credentials.');
    } else {
      console.log('An error occurred:', error.message);
    }
  }
};```
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