aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/www/api
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2021-10-18 00:40:10 +0300
committerGravatar Vitaly Takmazov2021-10-18 02:10:01 +0300
commita2348e95bbacbc9a9961afcabb7bbe82f3c7fe9d (patch)
tree7c794eba8ce72e6533a818fcbfb3c8f87e36d2f8 /src/main/java/com/juick/www/api
parent6c82f3dea2c56e8d62bd2f154e2e701f96454418 (diff)
Display error for social login
* CrossPostService gone
Diffstat (limited to 'src/main/java/com/juick/www/api')
-rw-r--r--src/main/java/com/juick/www/api/ApiSocialLogin.java44
-rw-r--r--src/main/java/com/juick/www/api/Users.java12
2 files changed, 25 insertions, 31 deletions
diff --git a/src/main/java/com/juick/www/api/ApiSocialLogin.java b/src/main/java/com/juick/www/api/ApiSocialLogin.java
index 4b57ce89..ecd5ac1b 100644
--- a/src/main/java/com/juick/www/api/ApiSocialLogin.java
+++ b/src/main/java/com/juick/www/api/ApiSocialLogin.java
@@ -30,7 +30,6 @@ import com.github.scribejava.core.oauth.OAuth20Service;
import com.juick.model.AuthResponse;
import com.juick.model.ext.facebook.User;
import com.juick.model.ext.vk.UsersResponse;
-import com.juick.service.CrosspostService;
import com.juick.service.EmailService;
import com.juick.service.UserService;
import com.juick.util.HttpBadRequestException;
@@ -94,8 +93,6 @@ public class ApiSocialLogin {
private String baseUri;
@Inject
- private CrosspostService crosspostService;
- @Inject
private UserService userService;
@Inject
private EmailService emailService;
@@ -133,11 +130,11 @@ public class ApiSocialLogin {
@RequestParam(required = false) String state) throws IOException, ExecutionException, InterruptedException {
if (StringUtils.isBlank(code)) {
String fbstate = UUID.randomUUID().toString();
- crosspostService.addFacebookState(fbstate, state);
+ userService.addFacebookState(fbstate, state);
return "redirect:" + facebookAuthService.getAuthorizationUrl(fbstate);
}
- String redirectUrl = crosspostService.verifyFacebookState(state);
+ String redirectUrl = userService.verifyFacebookState(state);
if (StringUtils.isEmpty(redirectUrl)) {
logger.error("state is missing");
@@ -158,26 +155,25 @@ public class ApiSocialLogin {
throw new HttpBadRequestException();
}
- int uid = crosspostService.getUIDbyFBID(fbID);
- if (uid > 0) {
- if (!crosspostService.updateFacebookUser(fbID, token.getAccessToken(), fb.getName())) {
+ Optional<com.juick.model.User> existingFacebookUser = userService.getUserByFacebookId(fbID);
+ if (existingFacebookUser.isPresent()) {
+ if (!userService.updateFacebookUser(fbID, token.getAccessToken(), fb.getName())) {
logger.error("error updating facebook user, id: {}, token: {}", fbID, token.getAccessToken());
throw new HttpBadRequestException();
}
+ if (StringUtils.isNotEmpty(fb.getEmail())) {
+ logger.info("found {} for facebook user {}", fb.getEmail(), fb.getName());
+ Integer userId = existingFacebookUser.get().getUid();
+ if (!emailService.getEmails(userId, false).contains(fb.getEmail())) {
+ emailService.addEmail(userId, fb.getEmail());
+ }
+ }
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl);
- uriComponentsBuilder.queryParam("hash", userService.getHashByUID(uid));
+ uriComponentsBuilder.queryParam("hash", userService.getHashByUID(existingFacebookUser.get().getUid()));
uriComponentsBuilder.queryParam("retpath", redirectUrl);
return "redirect:" + uriComponentsBuilder.build().toUriString();
} else {
- if (!crosspostService.createFacebookUser(fbID, state, token.getAccessToken(), fb.getName())) {
- if (StringUtils.isNotEmpty(fb.getEmail())) {
- logger.info("found {} for facebook user {}", fb.getEmail(), fb.getName());
- Integer userId = crosspostService.getUIDbyFBID(fbID);
- if (!emailService.getEmails(userId, false).contains(fb.getEmail())) {
- emailService.addEmail(userId, fb.getEmail());
- }
- }
- logger.info("email not found for facebook user {}", fb.getName());
+ if (!userService.createFacebookUser(fbID, state, token.getAccessToken(), fb.getName())) {
throw new HttpBadRequestException();
}
return "redirect:/signup?type=fb&hash=" + state;
@@ -188,11 +184,11 @@ public class ApiSocialLogin {
@RequestParam String state) throws IOException, ExecutionException, InterruptedException {
if (StringUtils.isBlank(code)) {
String vkstate = UUID.randomUUID().toString();
- crosspostService.addVKState(vkstate, state);
+ userService.addVKState(vkstate, state);
return "redirect:" + vkAuthService.getAuthorizationUrl(vkstate);
}
- String redirectUrl = crosspostService.verifyVKState(state);
+ String redirectUrl = userService.verifyVKState(state);
if (StringUtils.isBlank(redirectUrl)) {
logger.error("state is missing");
throw new HttpBadRequestException();
@@ -213,7 +209,7 @@ public class ApiSocialLogin {
}
long vkID = NumberUtils.toLong(jsonUser.getId(), 0);
- int uid = crosspostService.getUIDbyVKID(vkID);
+ int uid = userService.getUIDbyVKID(vkID);
if (uid > 0) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl);
uriComponentsBuilder.queryParam("hash", userService.getHashByUID(uid));
@@ -221,7 +217,7 @@ public class ApiSocialLogin {
return "redirect:" + uriComponentsBuilder.build().toUriString();
} else {
String loginhash = UUID.randomUUID().toString();
- if (!crosspostService.createVKUser(vkID, loginhash, token.getAccessToken(), vkName, vkLink)) {
+ if (!userService.createVKUser(vkID, loginhash, token.getAccessToken(), vkName, vkLink)) {
logger.error("create vk user error");
throw new HttpBadRequestException();
}
@@ -270,7 +266,7 @@ public class ApiSocialLogin {
public String doAppleLogin(@RequestParam(required = false) String code, @RequestParam String state) {
if (StringUtils.isBlank(code)) {
String astate = UUID.randomUUID().toString();
- crosspostService.addVKState(astate, state);
+ userService.addVKState(astate, state);
return "redirect:" + appleSignInService.getAuthorizationUrl(astate);
}
throw new HttpBadRequestException();
@@ -287,7 +283,7 @@ public class ApiSocialLogin {
if (email.isPresent()) {
com.juick.model.User user = userService.getUserByEmail(email.get());
if (!user.isAnonymous()) {
- String redirectUrl = crosspostService.verifyVKState(body.get("state"));
+ String redirectUrl = userService.verifyVKState(body.get("state"));
if (StringUtils.isBlank(redirectUrl)) {
logger.error("state is missing");
throw new HttpBadRequestException();
diff --git a/src/main/java/com/juick/www/api/Users.java b/src/main/java/com/juick/www/api/Users.java
index 06d040ff..0294ec53 100644
--- a/src/main/java/com/juick/www/api/Users.java
+++ b/src/main/java/com/juick/www/api/Users.java
@@ -55,8 +55,6 @@ public class Users {
@Inject
private MessagesService messagesService;
@Inject
- private CrosspostService crosspostService;
- @Inject
private TelegramService telegramService;
@Inject
private EmailService emailService;
@@ -151,10 +149,10 @@ public class Users {
if (StringUtils.isNotEmpty(accountToDelete)) {
switch (accountToDelete) {
case "twitter":
- crosspostService.deleteTwitterToken(visitor.getUid());
+ userService.deleteTwitterToken(visitor.getUid());
break;
case "vk":
- crosspostService.deleteVKUser(visitor.getUid());
+ userService.deleteVKUser(visitor.getUid());
break;
case "durov":
telegramService.deleteTelegramUser(visitor.getUid());
@@ -267,13 +265,13 @@ public class Users {
return emailService.getNotificationsEmail(this.getUid());
}
public String getTwitterName() {
- return crosspostService.getTwitterName(this.getUid());
+ return userService.getTwitterName(this.getUid());
}
public String getTelegramName() {
- return crosspostService.getTelegramName(this.getUid());
+ return userService.getTelegramName(this.getUid());
}
public ApplicationStatus getFacebookStatus() {
- return crosspostService.getFbCrossPostStatus(this.getUid());
+ return userService.getFbCrossPostStatus(this.getUid());
}
}
}