Username and Email field in the User model

I have a question regarding the User model, which I'm not sure it if's part of the standard Wasp setup or OpenSaaS specific. By default, they contain the user's email. I'd like the user to be able to change their username, as there is no need to repeat the same information twice, but I want to make sure it's safe to do so. What surprises me is that the username field in the User model is marked as @unique, which suggests it might be used for login purposes.
4 Replies
kapa.ai
kapa.ai2mo ago
Hi @Sergio Visinoni. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, the User model is used to store data relevant to your application. The username field in the User model is marked as @unique to ensure that each user has a unique username in the system. This is not necessarily used for login purposes, but rather to maintain uniqueness of usernames among users. If you want to allow users to change their username, you can do so. However, you need to ensure that the new username they choose is not already taken by another user. This is where the @unique constraint comes into play - it will prevent a new user from choosing a username that is already in use. In terms of safety, as long as you handle the username change process correctly (i.e., validate the new username, check for uniqueness, etc.), it should be safe to allow users to change their username. Regarding the email field, Wasp supports multiple different authentication methods. If you are using the Username & password (https://wasp-lang.dev/docs/auth/username-and-pass) authentication method, you need to store the user's username and password. On the other hand, if you are using the Email (https://wasp-lang.dev/docs/auth/email) authentication method, you will need to store the user's email, password, and for example, their email verification status. So, the fields you need in the User model depend on the authentication method you choose. Here is an example of a User model with a unique username field:
entity User {=psl
id Int @id @default(autoincrement())
username String @unique
psl=}
entity User {=psl
id Int @id @default(autoincrement())
username String @unique
psl=}
Sources: - Wasp Auth Entities (https://wasp-lang.dev/docs/auth/entities) - Migration from 0.11.X to 0.12.X (https://wasp-lang.dev/docs/migrate-from-0-11-to-0-12#new-auth) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Sergio Visinoni, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos2mo ago
Kapa is quite correct here! Short, username in OpenSaas has @unique on it because that in general makes sense, not because of a requirement by Wasp Auth: In OpenSaas, we come with email&pass auth enabled, which doesn't need username. If you used username&pass auth, that would reuiqre username, but that is not the case with initial state of OpenSaas. Neither do the social auth's taht are easy to comment out need it. I do believe we use username in a couple of other places in the opensaas, but if I am correct, only for displaying it, nothing crucial. So if you wanted to, you should be able to remove it from the entity User (and remove its usage from the JS code).
Sergio Visinoni
Sergio VisinoniOP2mo ago
Thanks a lot for the clarification @martinsos !
MEE6
MEE62mo ago
Wohooo @Sergio Visinoni, you just became a Waspeteer level 3!
Want results from more Discord servers?
Add your server