#why getUserName return email ?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
The getUserName() method in the UserDetails interface does not necessarily return the email. The purpose of this method is to return the username associated with the user account.
However, in some implementations of the UserDetails interface, such as User class provided by Spring Security, the username is often set to be an email address. This is a common practice since email addresses are unique and can serve as a good identifier for user accounts.
So, if you are using a specific implementation of the UserDetails interface that sets the username to be an email address, then it is correct for the getUserName() method to return the email. But keep in mind that this behavior may vary depending on how you have implemented or configured your user details object.
Is that code you wrote yourself?
^^^ however in some apps the username is the same as the email
but you should know that yourself
nope, this is from a course on amigoscode's youtube
can you explain it ?
thank you. I just ask for review
A method called getUserName should return a username lol, but as already said it depends on the context
yes, but if i want to when i register with email verification so it should return email ? sorry for dumb question. This is just a course i learn from youtube
A method returning the email should be called like getEmail though
As Squid said, in code it's better to have a method return exactly what it says
If you don't have a separate username value, and your application is supposed to use the user's email as their username this may be valid.
But only in case you really can't rename that method to getEmail
Don't worry, it's completely fine. the implementation forces the user to login using username and password but if you want to make it work with email instead then treating username as email works.
i'm appreciate to hear your explain
thank you so much... i'm beginner so this is a dumb question with you guys but i just want to know why for improve my logical thinking in coding
Detected code, here are some useful tools:
But only incase you really can't rename that method to`getEmail`
in case i want to the user register with username or email, so then i have to create two method for that case ? one for return username and one for email register ? is that right ?
I'm not sure why would you exactly want the user to register with either email or username I'd personally force him to fill both when registering. But if you meant to login with either username or email then you'll have to implement UserDetailsServce where loading the user by username would first check if it's an email to load the user by or treat it as username login otherwise.
You'll also have to also take into consideration when registering a user that the username must be unique for each user to be able to load the user on login later on, same concept applies to email.