Hi everyone, i’m looking for the best practices about manage the application User. In particular would like to give the opportunity to the user to change itself username, name and password and to the admin to create and modify the users data.
I don’t know if there is something already done about a application user management (maybe there is a offical way??). I’m using hilla and this is how i thought to implement the change password:
i had create a new field (password) to help to send iinfromation from front end to backend methods to get new password and encrypt it
User.js
....
private String password;
@JsonIgnore
private String hashedPassword;
....
public void setPassword(String password) {
if (!Objects.isNull(password) && !password.isEmpty() && !password.isBlank()) {
var bc = new BCryptPasswordEncoder();
var newHashedPwd = bc.encode(password);
setHashedPassword(newHashedPwd);
}
}
to edit and create user I use standard Userservice.java
public User update(User entity) {
return repository.save(entity);
}
in the front end i use useForm hook to manage an edit form with a modal
const { model, submit, field, read, value, setValue, clear } = useForm(UserModel, {
onSubmit: async (data) => {
console.log("Saving user: ", data )
await UserService.update(data).then(() => {clear();setVisibleEdit(false)});
}
});
this scenario works but i have a problem with hashedPassword, because hashedPassword is ingnored by @Jsonignore so in my User model there isn’t hashed password field so repository.save(entity); set hashed password field to null.
There is a way to prevent to set null unsetted field or i should create a custom method?
The same is for new User, thats give me an error becuuse id and version si unsetted
function createNewUser() {
const newUser: User = UserModel.createEmptyValue();
newUser.id=-1;
newUser.version=-1
read(newUser)
setVisibleEdit(true);
}
Thanks