mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 06:20:59 +08:00
use platform avatar for oauth registration
This commit is contained in:
@@ -60,6 +60,7 @@ public class GithubAuthService {
|
||||
}
|
||||
JsonNode userNode = userRes.getBody();
|
||||
String username = userNode.hasNonNull("login") ? userNode.get("login").asText() : null;
|
||||
String avatarUrl = userNode.hasNonNull("avatar_url") ? userNode.get("avatar_url").asText() : null;
|
||||
String email = null;
|
||||
if (userNode.hasNonNull("email")) {
|
||||
email = userNode.get("email").asText();
|
||||
@@ -83,13 +84,13 @@ public class GithubAuthService {
|
||||
if (email == null) {
|
||||
email = username + "@users.noreply.github.com";
|
||||
}
|
||||
return Optional.of(processUser(email, username, mode));
|
||||
return Optional.of(processUser(email, username, avatarUrl, mode));
|
||||
} catch (Exception e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private User processUser(String email, String username, com.openisle.model.RegisterMode mode) {
|
||||
private User processUser(String email, String username, String avatar, com.openisle.model.RegisterMode mode) {
|
||||
Optional<User> existing = userRepository.findByEmail(email);
|
||||
if (existing.isPresent()) {
|
||||
User user = existing.get();
|
||||
@@ -113,7 +114,11 @@ public class GithubAuthService {
|
||||
user.setRole(Role.USER);
|
||||
user.setVerified(true);
|
||||
user.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
||||
user.setAvatar("https://github.com/" + finalUsername + ".png");
|
||||
if (avatar != null) {
|
||||
user.setAvatar(avatar);
|
||||
} else {
|
||||
user.setAvatar("https://github.com/" + finalUsername + ".png");
|
||||
}
|
||||
return userRepository.save(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,13 +35,14 @@ public class GoogleAuthService {
|
||||
GoogleIdToken.Payload payload = idToken.getPayload();
|
||||
String email = payload.getEmail();
|
||||
String name = (String) payload.get("name");
|
||||
return Optional.of(processUser(email, name, mode));
|
||||
String picture = (String) payload.get("picture");
|
||||
return Optional.of(processUser(email, name, picture, mode));
|
||||
} catch (Exception e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private User processUser(String email, String name, com.openisle.model.RegisterMode mode) {
|
||||
private User processUser(String email, String name, String avatar, com.openisle.model.RegisterMode mode) {
|
||||
Optional<User> existing = userRepository.findByEmail(email);
|
||||
if (existing.isPresent()) {
|
||||
User user = existing.get();
|
||||
@@ -66,7 +67,11 @@ public class GoogleAuthService {
|
||||
user.setRole(Role.USER);
|
||||
user.setVerified(true);
|
||||
user.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
||||
user.setAvatar("https://github.com/identicons/" + username + ".png");
|
||||
if (avatar != null) {
|
||||
user.setAvatar(avatar);
|
||||
} else {
|
||||
user.setAvatar("https://github.com/identicons/" + username + ".png");
|
||||
}
|
||||
return userRepository.save(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class TwitterAuthService {
|
||||
try {
|
||||
logger.debug("Fetching user info with access token");
|
||||
userRes = restTemplate.exchange(
|
||||
"https://api.twitter.com/2/users/me",
|
||||
"https://api.twitter.com/2/users/me?user.fields=profile_image_url",
|
||||
HttpMethod.GET,
|
||||
new HttpEntity<>(authHeaders),
|
||||
JsonNode.class);
|
||||
@@ -98,6 +98,7 @@ public class TwitterAuthService {
|
||||
|
||||
JsonNode data = userRes.getBody() == null ? null : userRes.getBody().path("data");
|
||||
String username = data != null ? data.path("username").asText(null) : null;
|
||||
String avatar = data != null ? data.path("profile_image_url").asText(null) : null;
|
||||
if (username == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -105,10 +106,10 @@ public class TwitterAuthService {
|
||||
// Twitter v2 默认拿不到 email;如果你申请到 email.scope,可改用 /2/users/:id?user.fields=email
|
||||
String email = username + "@twitter.com";
|
||||
logger.debug("Processing user {} with email {}", username, email);
|
||||
return Optional.of(processUser(email, username, mode));
|
||||
return Optional.of(processUser(email, username, avatar, mode));
|
||||
}
|
||||
|
||||
private User processUser(String email, String username, com.openisle.model.RegisterMode mode) {
|
||||
private User processUser(String email, String username, String avatar, com.openisle.model.RegisterMode mode) {
|
||||
Optional<User> existing = userRepository.findByEmail(email);
|
||||
if (existing.isPresent()) {
|
||||
User user = existing.get();
|
||||
@@ -133,7 +134,11 @@ public class TwitterAuthService {
|
||||
user.setRole(Role.USER);
|
||||
user.setVerified(true);
|
||||
user.setApproved(mode == com.openisle.model.RegisterMode.DIRECT);
|
||||
user.setAvatar("https://twitter.com/" + finalUsername + "/profile_image");
|
||||
if (avatar != null) {
|
||||
user.setAvatar(avatar);
|
||||
} else {
|
||||
user.setAvatar("https://twitter.com/" + finalUsername + "/profile_image");
|
||||
}
|
||||
logger.debug("Creating new user {}", finalUsername);
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user