Skip to content

Commit

Permalink
Refactor getCurrentUser method to return JWT instead of User object
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiUpadhyayula authored Mar 12, 2022
1 parent 52cc5a2 commit 4f389dc
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -58,10 +59,10 @@ public void signup(RegisterRequest registerRequest) {

@Transactional(readOnly = true)
public User getCurrentUser() {
org.springframework.security.core.userdetails.User principal = (org.springframework.security.core.userdetails.User) SecurityContextHolder.
Jwt principal = (Jwt) SecurityContextHolder.
getContext().getAuthentication().getPrincipal();
return userRepository.findByUsername(principal.getUsername())
.orElseThrow(() -> new UsernameNotFoundException("User name not found - " + principal.getUsername()));
return userRepository.findByUsername(principal.getSubject())
.orElseThrow(() -> new UsernameNotFoundException("User name not found - " + principal.getSubject()));
}

private void fetchUserAndEnable(VerificationToken verificationToken) {
Expand Down

0 comments on commit 4f389dc

Please sign in to comment.