How to add roles for user while user registration form angular

I’m working on a Spring Boot based web application in Java. I’m trying to create admin/user registration for my application. To be honest this is my first application and I don’t really know how to add user roles while registration in front end whereas I did it in back end role added without problem. I’ve searched about this issue, but no one helps me how to add roles.

In my database it added like user how ever the input are.

If someone tell me you should add the roles in constructor’s user, the role in user is Set while input value is Set String> it should convert it first to a Set and add it to user.setRole(x) then I can use it but how can I use it in front end? that’s my problem.

And Thank you in advance to any one who may be able to give me some ideas.


    public ResponseEntity<?> registerUser(@Valid @RequestBody RegistrationForm signUpRequest) {
       if (utilisateurRepository.existsByUsername(signUpRequest.getUsername())) {
            return ResponseEntity
                    .body(new MessageResponse("Error: Username is already taken!"));

        if (utilisateurRepository.existsByEmail(signUpRequest.getEmail())) {
            return ResponseEntity
                    .body(new MessageResponse("Error: Email is already in use!"));

        // Create new user's account
        Utilisateur user = new Utilisateur(signUpRequest.getUsername(),
                passwordEncoder.encode(signUpRequest.getPassword()), signUpRequest.getTelephone());

        Set<String> strRoles = signUpRequest.getRoles();
        Set<Role> roles = new HashSet<>();

        if (strRoles == null) {
            Role userRole = roleRepository.findByName(ERole.ROLE_USER)
                    .orElseThrow(() -> new RuntimeException("Error: Role is not found."));

        } else {
            strRoles.forEach(role -> {
                switch (role) {
                    case "admin":
                        Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN)
                                .orElseThrow(() -> new RuntimeException("Error: Role is not found."));


                        Role aideRole = roleRepository.findByName(ERole.ROLE_AIDESOIGNANTE )
                                .orElseThrow(() -> new RuntimeException("Error: Role is not found."));


Document(collection =  "Utilisateur")
public class Utilisateur {

    private String id_user;
    @Size(max = 20)
    private String username;

    private String password;
    @Size(max = 20)
    private String Nom;

    @Size(max = 50)
    private String email;
    private int telephone;

    public Utilisateur(String username, String email, String password, int telephone) {
        this.username = username; = email;
        this.telephone = telephone;
        this.password = password;

    private Set<Role> roles = new HashSet<>();


Auth.Service => Angular

  providedIn: 'root'
export class AuthService {

  role : any [];

  constructor(private http: HttpClient) { }

  register(user): Observable<any> {
    return + 'signup', {
      username: user.username,
      telephone: user.telephone,
      role: user.role,
      password: user.password
    }, httpOptions);
1 Like