2021-06-28

How to send data from the database to the front end

Tell me how to get all its data from the database correctly by login. I wrote a code that checks the presence of a given login in the database. Next, I need to return all related information with this login to the frontend. it can be his description, mail, phone number, second name.

authorization:

@PostMapping("login") public ResponseEntity<?> login(@RequestBody AuthenticationRequestDto requestDto) {

  try {
      String username = requestDto.getUsername();
      authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, requestDto.getPassword()));
      User user = userRepository.findByUsername(username);
      

      if (user == null) {
          throw new UsernameNotFoundException("User with username: " + username + " not found");
      }
      
      String token = jwtTokenProvider.createToken(username, user.getRoles());
      
    
      return ResponseEntity.ok(new JwtUser(
              token,
              user.getId(),
              user.getUsername(),
              user.getSecondName(),
              user.getPhone(),
              user.getDescription(),
              user.getEmail(),
              user.getRoles())); 
      
  } catch (AuthenticationException e) {
      throw new BadCredentialsException("Invalid username or password");
  }

}

to create a user object:

public class JwtUser implements UserDetails {

private String token;

private final Long id;

private final String username;
private  String secondName;

private  String password;
    
private  String phone;


private  String description;
        
private  String email;
        
private  boolean active;

private  Date lastPasswordResetDate;

private final Collection<? extends GrantedAuthority> authorities;



public JwtUser(Long id, String username, String secondName, String password, String phone, String description,
        String email, boolean active, Date lastPasswordResetDate,
        Collection<? extends GrantedAuthority> authorities) {
    
    this.id = id;
    this.username = username;
    this.secondName = secondName;
    this.password = password;
    this.phone = phone;
    this.description = description;
    this.email = email;
    this.active = active;
    this.lastPasswordResetDate = lastPasswordResetDate;
    this.authorities = authorities;
}



public JwtUser(String token, Long id, String username, String secondName,  String phone, String description,
        String email, Collection<? extends GrantedAuthority> authorities) {
    this.token=token;
    this.id = id;
    this.username = username;
    this.secondName = secondName;       
    this.phone = phone;
    this.description = description;
    this.email = email;     
    this.authorities = authorities;
}





public String getToken() {
    return token;
}

public void setToken(String token) {
    this.token = token;
}

@JsonIgnore 
public Long getId() {
    return id;
}

public String getSecondName() {
    return secondName;
}

public String getPhone() {
    return phone;
}

public String getDescription() {
    return description;
}

public String getEmail() {
    return email;
}

public boolean isActive() {
    return active;
}
@JsonIgnore
public Date getLastPasswordResetDate() {
    return lastPasswordResetDate;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    
    return authorities;
}
@JsonIgnore
@Override
public String getPassword() {
    
    return password;
}

@Override
public String getUsername() {
    
    return username;
}
@JsonIgnore
@Override
public boolean isAccountNonExpired() {
    
    return true;
}
@JsonIgnore
@Override
public boolean isAccountNonLocked() {
    
    return true;
}
@JsonIgnore
@Override
public boolean isCredentialsNonExpired() {
    
    return true;
}

@Override
public boolean isEnabled() {
    
    return true;
}

}

database:

public interface UserRepository extends JpaRepository<User, Long>{
    
    User findByUsername(String name);
    

}

the authorization code is incorrect, since the constructor that collects the object to send it to the frontend does not understand what values to set. How to create an object using an authorized login?



from Recent Questions - Stack Overflow https://ift.tt/2U0zWdP
https://ift.tt/eA8V8J

No comments:

Post a Comment