Get facebook social username, email, name and process password -> build
public class OAuth2DetailsService extends DefaultOAuth2UserService {
private final UserRepository userRepository;
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
Map<String, Object> userInfo = oAuth2User.getAttributes();
String username = "facebook_" + (String) userInfo.get("id");
String password = new BCryptPasswordEncoder().encode(UUID.randomUUID().toString());
String email = (String) userInfo.get("email");
String name = (String) userInfo.get("name");
User userEntity = userRepository.findByUsername(username);
if(userEntity == null) {
User user = User.builder()
.username(username)
.password(password)
.email(email)
.name(name)
.role("ROLE_USER")
.build();
return new PrincipalDetails(userRepository.save(user), oAuth2User.getAttributes());
} else {
return new PrincipalDetails(userEntity, oAuth2User.getAttributes());
}
}
}
Make an overload
public PrincipalDetails(User user, Map<String, Object> attributes) {
this.user = user;
}
Catch the facebook login users to append at PrincipalDetails data.