$user->id() as userid in database: bad idea?

Hey,

I plan on using the kirby user-id - which looks something like this: Zwi5ySB0 - for a unique-userid-field in a database. Is this maybe a bad idea?

  • if so: why?
  • if not: does the id-string have a defined length? How is it created?

Thank you for the help.

Ralf

The user ID never changes while the user exists, so yes, you can use it to identify a user in a database. This user ID is also what is stored when you use Kirby’s users field.

This is the method that creates the user id:

/**
     * Returns a random user id
     *
     * @return string
     */
    public function createId(): string
    {
        $length = 8;
        $id     = Str::random($length);

        while ($this->kirby()->users()->has($id)) {
            $length++;
            $id = Str::random($length);
        }

        return $id;
    }

So the ID can have more than 8 characters when the random ID created by Str::random is already taken.

Thank you for the quick reply.

I actually had a look at the sourcecode of createId, but I must have overseen $length . shsh
So to me it looks as if I’m on the safe side when i use a varchar(15) for the user-id.

thank you and good night.

Yes, that will definitely be sufficient.

:sleeping: