-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74b159f
commit 54538f5
Showing
8 changed files
with
127 additions
and
66 deletions.
There are no files selected for viewing
24 changes: 12 additions & 12 deletions
24
src/main/java/com/yevhenii/kpi/readmore/FacebookConnectionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package com.yevhenii.kpi.readmore; | ||
|
||
import org.springframework.social.connect.support.OAuth2ConnectionFactory; | ||
import org.springframework.social.facebook.api.Facebook; | ||
import org.springframework.social.facebook.connect.FacebookAdapter; | ||
import org.springframework.social.facebook.connect.FacebookServiceProvider; | ||
|
||
public class FacebookConnectionFactory extends OAuth2ConnectionFactory<Facebook> { | ||
public FacebookConnectionFactory(String clientId, String clientSecret) { | ||
super("facebook", new FacebookServiceProvider(clientId, clientSecret, ""), new FacebookAdapter()); | ||
} | ||
} | ||
//package com.yevhenii.kpi.readmore; | ||
// | ||
//import org.springframework.social.connect.support.OAuth2ConnectionFactory; | ||
//import org.springframework.social.facebook.api.Facebook; | ||
//import org.springframework.social.facebook.connect.FacebookAdapter; | ||
//import org.springframework.social.facebook.connect.FacebookServiceProvider; | ||
// | ||
//public class FacebookConnectionFactory extends OAuth2ConnectionFactory<Facebook> { | ||
// public FacebookConnectionFactory(String clientId, String clientSecret) { | ||
// super("facebook", new FacebookServiceProvider(clientId, clientSecret, ""), new FacebookAdapter()); | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/yevhenii/kpi/readmore/controller/OAuthSignUpController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.yevhenii.kpi.readmore.controller; | ||
|
||
import com.yevhenii.kpi.readmore.service.OAuthSignUpService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
@Controller | ||
public class OAuthSignUpController { | ||
|
||
private final OAuthSignUpService signUpService; | ||
|
||
@Autowired | ||
public OAuthSignUpController(OAuthSignUpService signUpService) { | ||
this.signUpService = signUpService; | ||
} | ||
|
||
// todo add dao | ||
@RequestMapping("/signup") | ||
public ModelAndView signup(WebRequest request) { | ||
|
||
if (signUpService.signup(request)) { | ||
return new ModelAndView("redirect:/"); | ||
} else { | ||
return new ModelAndView("redirect:/error"); | ||
} | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
src/main/java/com/yevhenii/kpi/readmore/controller/SignUpController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/yevhenii/kpi/readmore/service/OAuthSignUpService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.yevhenii.kpi.readmore.service; | ||
|
||
import com.yevhenii.kpi.readmore.model.User; | ||
import com.yevhenii.kpi.readmore.repository.UserRepository; | ||
import com.yevhenii.kpi.readmore.security.AuthUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.ConnectionFactoryLocator; | ||
import org.springframework.social.connect.UsersConnectionRepository; | ||
import org.springframework.social.connect.web.ProviderSignInUtils; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import java.util.Objects; | ||
|
||
@Service | ||
public class OAuthSignUpService { | ||
|
||
private final ProviderSignInUtils signInUtils; | ||
private final UserRepository userRepository; | ||
|
||
@Autowired | ||
public OAuthSignUpService(ConnectionFactoryLocator connectionFactoryLocator, | ||
UsersConnectionRepository connectionRepository, | ||
UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
this.signInUtils = new ProviderSignInUtils(connectionFactoryLocator, connectionRepository); | ||
} | ||
|
||
// todo add dao | ||
// todo think about duplicated usernames | ||
public boolean signup(WebRequest request) { | ||
Connection<?> connection = signInUtils.getConnectionFromSession(request); | ||
|
||
if (Objects.isNull(connection)){ | ||
return false; | ||
} | ||
|
||
String name = connection.getDisplayName().substring(1); | ||
|
||
userRepository.findUserByName(name).orElseGet(() -> { | ||
User user = User.builder() | ||
.name(name) | ||
.strategy("twitter") | ||
.build(); | ||
|
||
return userRepository.save(user); | ||
}); | ||
|
||
AuthUtils.authenticate(connection); | ||
signInUtils.doPostSignUp(connection.getDisplayName(), request); | ||
|
||
return true; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.