I am developing an application using JHispter 3.12.2.
Since I needed more information than JHipster, I created a UserExtra object containing two lines: the phone number and the skype address. I associated this entity with JHI_User in a one-to-one relationship.
Now the problem I am facing is when a user logs in, I want to create a new UserExtra associated with the registered user.
To achieve this, I tried to perform a few client-side actions.
How the standard JHipster login page works, a variable called vm.registerAccount is used that contains the name firstName, lastName, login, password, etc.
I tried using another vm.userExtra variable containing my phone number and skype. Then I tried a few things:
In register.controller.js, I passed my userExtra to the createAccount function:
Auth.createAccount (vm.registerAccount, vm.userExtra) .then (function () {
vm.success = 'OK';
}). catch (function (response) {
vm.success = null;
if (response.status === 400 && response.data === 'login already in use') {
vm.errorUserExists = 'ERROR';
} else if (response.status === 400 && response.data === 'e-mail address already in use') {
vm.errorEmailExists = 'ERROR';
} else {
vm.error = 'ERROR';
}
});
I changed the createAccount auth.service.js function to make the changes made:
function createAccount (account, userExtra, callback) {
var cb = callback || angular.noop;
return Register.save(account, userExtra, function () { return cb(account); }, function (err) { this.logout(); return cb(err); }.bind(this)).$promise; }</pre>
Finally, I updated the registerAccount AccountResource.java function on the server side:
public ResponseEntity registerAccount (@Valid @RequestBody ManagedUserVM managedUserVM, UserExtra userExtra) {...}
But the registerAccount function was not even executed from what I remember.
I also tried adding a new user to the createAccount register.controller.js function callback as follows:
Auth.createAccount(vm.registerAccount).then(function (result) { vm.userExtra.user = result; UserExtra.save(vm.userExtra, function() {
But I was getting an error while trying to save a new UserExtra.
I'm sure I need to change the registerAccount () function inside AccountResource.java, but I just canβt get the additional information I'm trying to send from the client side. I managed to create a new user inside this function and associate it with JHI_User, but without additional information.
Most likely, someone has already encountered this problem, what could be the best solution to this problem?
Edit using solution:
Thanks to GaΓ«l Marziou, I fixed my problem.
Here is a minimal project example using the solution described here.
On the client side, on the register.html page that I rewrote for my use, I have two fields associated with the vm.registerAccount properties:
<input class="form-control" id="phone" ng-model="vm.registerAccount.phone" placeholder="{{'global.form.phone.placeholder' | translate}}" /> ... <input class="form-control" id="skype" ng-model="vm.registerAccount.skype" placeholder="{{'global.form.skype.placeholder' | translate}}" />
In ManagedUserVM, I just added two fields and their recipients:
private String phone; private String skype; public String getPhone() { return phone; } public String getSkype() { return skype; }
I modified my UserExtra class to map the User and UserExtra identifiers so that they are mirrored. This speeds up the search process and makes more sense, since UserExtra is really just an extension for the user:
public class UserExtra implements Serializable { private static final long serialVersionUID = 1L; @Id private Long id; @Column(name = "phone") private String phone; @Column(name = "skype") private String skype; @OneToOne @MapsId private User user; ... }
I created a new user function called createUser () in the UserService, which needs my two fields in addition to the base ones. I did not update the existing function, so I do not need to change the test classes:
public User createUser(String login, String password, String firstName, String lastName, String email, String langKey, String phone, String skype) { User newUser = new User(); Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER); Set<Authority> authorities = new HashSet<>(); String encryptedPassword = passwordEncoder.encode(password); newUser.setLogin(login);
Finally, I updated the registerAccount () function of AccountResource to call my custom function using two additional fields:
public ResponseEntity<?> registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) { HttpHeaders textPlainHeaders = new HttpHeaders(); textPlainHeaders.setContentType(MediaType.TEXT_PLAIN); return userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()) .map(user -> new ResponseEntity<>("login already in use", textPlainHeaders, HttpStatus.BAD_REQUEST)) .orElseGet(() -> userRepository.findOneByEmail(managedUserVM.getEmail()) .map(user -> new ResponseEntity<>("e-mail address already in use", textPlainHeaders, HttpStatus.BAD_REQUEST)) .orElseGet(() -> { User user = userService .createUser(managedUserVM.getLogin(), managedUserVM.getPassword(), managedUserVM.getFirstName(), managedUserVM.getLastName(), managedUserVM.getEmail().toLowerCase(), managedUserVM.getLangKey(), managedUserVM.getPhone(), managedUserVM.getSkype()); mailService.sendActivationEmail(user); return new ResponseEntity<>(HttpStatus.CREATED); }) ); }