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
- Couldnt not do appwrite push tables
Not sure why i am not able to create my tables
- Problem with Google Workspace at DNS Rec...
Hello, I bought a domain at Namecheap, and Google Workspace used to work there, but now that I switched from Custom DNS to Appwrite's nameservers, it doesn't w...
- change role of a team member in Appwrite
It's not possible to add/change roles of a team meber in Appwrite Frontend. When you click on a member of a team you get forwarded to the configuration page of ...