Skip to content
Back

sveltekit + appwrite make username unique

  • 0
  • Databases
  • Web
mina99
29 Mar, 2025, 11:03

hi guys i got a small question: How do i make my users usernames unique i dont want to have a 2 or more users with same username, this is my code

TypeScript
import { fail } from '@sveltejs/kit';
import { Client, Databases, ID } from 'appwrite';

export const actions = {
    register: async ({ request }) => {
        const data = await request.formData();
        const username = data.get('username');
        const password = data.get('password');
        const passwordRepeat = data.get('password-repeat');

        if (!username) {
            return fail(400, { message: 'Please enter a username' });
        }
        if (!password) {
            return fail(400, { message: 'Please enter a password' });
        }
        if (password !== passwordRepeat) {
            return fail(400, { message: 'Passwords do not match' });
        }

        try {
            const client = new Client()
                .setEndpoint('https://cloud.appwrite.io/v1')
                .setProject('xxx'); //OVO JE PROJECT ID
            const databases = new Databases(client);

            //PRVO JE DATABASE ID A DRUGO JE DOCUMENT ID

            databases.createDocument('xxxx', 'xxx', ID.unique(), {
                username,
                password
            });
        } catch (e) {
            console.log(e);
        }
    }
};

i tried to do the same thing with ID.unique() i tried the username.unique(), but it looks like it doesnt work, thanks in advance

TL;DR
To make usernames unique in a SvelteKit app using Appwrite, you can implement this logic in the register action. Ensure to check if the username already exists before creating a new document. You can query Appwrite to see if the username is already taken, or handle uniqueness at the database level.
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