Back

Creating A User Account

  • 0
  • Web
  • Databases
  • Auth
  • General
  • Cloud
DN Josh
29 Jan, 2025, 16:12

I have a simple form component that is expected to create a new user using the the ###firstName, ###lastName, ###username, and ###password from a react-hook-form application.

Is it possible to create this is user on the click of the form's submit button and have that user created on the appwrite account?

TL;DR
Code sample provided for creating a user account with React hook form. Missing implementation details, and not tested. Need to replace ###firstName, ###lastName, ###username, ###password with correct fields. Possible solution involves calling account.create and account.createEmailPasswordSession within onSubmit, then navigating to the app upon successful creation.
Kenny
29 Jan, 2025, 16:30

Sure, here is a code sample. Just an FYI I did not test this, it's just a concept of how you might accomplish this.

TypeScript
export default function LoginPage() {
    const { register, handleSubmit, formState: { errors } } = useForm();

    const onSubmit = (data) => {
      try {
        const user = await account.create(
            ID.unique(),
            data.email,
            data.password,
            data.name
        );
  
        const session = await account.createEmailPasswordSession(
            data.email,
            data.password
        );

        // navigate to app
      } catch (err) {
        console.error(err);
      }
    };

    return (
        <>
            <p className="title">Registration Form</p>

            <form onSubmit={handleSubmit(onSubmit)}>
                <input type="text" {...register("name")} />
                <input type="email" {...register("email")} />
                <input type="password" {...register("password")} />
                <input type={"submit"} />
            </form>
        </>
    );
}
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