private String getUsernameFromProfile(UserProfile profile) { String username; if (profile.getEmail() != null) { username = profile.getEmail(); } else if (profile.getUsername() != null) { username = profile.getUsername(); } else if (profile.getName() != null) { username = profile.getName(); } else { username = profile.toString(); } return username; }
@ApiOperation( value = "SNS 기반 회원 가입시 필요한 회원 프로필 정보", produces = "application/json", response = UserProfileForm.class) @RequestMapping(value = "/social/attempted", method = RequestMethod.GET) public UserProfileForm loginSocialUser(NativeWebRequest request) { Connection<?> connection = providerSignInUtils.getConnectionFromSession(request); if (Objects.isNull(connection)) throw new ServiceException(ServiceError.CANNOT_GET_SNS_PROFILE); ConnectionKey connectionKey = connection.getKey(); CommonConst.ACCOUNT_TYPE convertProviderId = CommonConst.ACCOUNT_TYPE.valueOf(connectionKey.getProviderId().toUpperCase()); UserProfile existUser = userService.findUserProfileByProviderIdAndProviderUserId( convertProviderId, connectionKey.getProviderUserId()); org.springframework.social.connect.UserProfile socialProfile = connection.fetchUserProfile(); String username = null; if (Objects.nonNull(socialProfile.getName())) { username = socialProfile.getName(); } else if (Objects.nonNull(socialProfile.getUsername())) { username = socialProfile.getUsername(); } else { if (Objects.nonNull(socialProfile.getFirstName())) { username = socialProfile.getFirstName(); } if (Objects.nonNull(socialProfile.getLastName())) { username = Objects.isNull(username) ? socialProfile.getLastName() : ' ' + socialProfile.getLastName(); } } UserProfileForm user = new UserProfileForm(); user.setEmail(socialProfile.getEmail()); user.setUsername(username); if (Objects.nonNull(existUser)) { user.setId(existUser.getId()); user.setAbout(existUser.getAbout()); if (Objects.nonNull(existUser.getSupportFC())) user.setFootballClub(existUser.getSupportFC().getId()); } return user; }
@Override public String execute(Connection<?> connection) { User user = new User(); UserProfile userProfile = connection.fetchUserProfile(); user.setName(userProfile.getName()); user.setEmail(userProfile.getEmail()); user.setImageUrl(connection.getImageUrl()); String username = getUsernameFromProfile(userProfile); user.setUsername(username); Set<String> auth = new HashSet<String>(); auth.add(Role.USER_ROLE); user.setAuthorities(auth); user.setEnabled(true); User createdUser = userRepository.store(user); return createdUser.getUsername(); }