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.Recommended threads
- TablesDB `updateRows` returns `database_...
Hi Appwrite team! I’m seeing a strange issue with TablesDB bulk row updates on a self-hosted Appwrite instance. **Environment** - Appwrite self-hosted `1.9.0` ...
- [SOLVED] Realtime Missing Channels
```js useEffect(() => { let subscription: RealtimeSubscription; async function loadChips() { try { const {rows: chi...
- Update row sheet not loading
After right clicking a row header and click update, the sidebar fails to load (never-ending skeleton)