Back

[SOLVED] Invalid userId:

  • 0
  • Web
Devben.tech
16 May, 2023, 19:05

Invalid userId: Parameter must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special character

That's the error am seeing when I tried to sign up using appwrite auth

TL;DR
The user encountered an error `Invalid userId` when trying to sign up using Appwrite authentication. The solution is to change the order of variables in the `create` function call. The correct order is `account.create(userId, email, password, username)`. The user provided their code snippet as an example. The error occurred because the userId parameter exceeded the character limit or contained invalid characters.
Binyamin
16 May, 2023, 19:08

Can you share the create function code?

Devben.tech
16 May, 2023, 19:11

This right

mport React, { useState } from "react"; import { account } from "./appwrite"; import "./auth.css" import { v4 as uuidv4 } from 'uuid';

function Signup() { const [username, setUsername] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState("");

const handleSignup = async (e) => { e.preventDefault();

TypeScript
try {
  // Validate inputs
  if (!username || !email || !password) {
    setError("Please fill in all fields");
    return;
  }

  // Call the createAccount method from appwrite object

  const randomString = Math.random().toString(36).substring(2, 15);

  // Combine username and random string to create user ID
  const userId = `${username.replace(/[^a-zA-Z0-9.-_]/g, "").toLowerCase()}_${randomString}`
  await account.create(email, password, username, userId);

  // User sign-up successful, perform further actions like redirecting to the main page
  // or updating the application state
  console.log("User signed up successfully");
} catch (error) {
  setError( error.message);
  console.error("Failed to sign up", error);
}

};

Devben.tech
16 May, 2023, 19:12

import { Client, Account } from 'appwrite';

const appwrite = new Client(); appwrite.setEndpoint('myendpoint'); appwrite.setProject('myid');

const account = new Account(appwrite);

export { appwrite, account };

Binyamin
16 May, 2023, 19:13

Everything seems fine, Just change the variables order in the function to this

TypeScript
      await account.create(userId, email, password, username);
Binyamin
16 May, 2023, 19:13
Devben.tech
16 May, 2023, 19:16

Wow! thank you it's working now

joeyouss
16 May, 2023, 19:29

Can you mark it as solved @Devben.tech ?

Devben.tech
16 May, 2023, 19:38

How do I do that ?

joeyouss
16 May, 2023, 19:39

I'll do it, np!

joeyouss
16 May, 2023, 19:39

[SOLVED] Invalid userId:

Devben.tech
16 May, 2023, 19:39

Okayt thanks

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