-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add social sign in functionality (Google, Facebook, Twitter) #2155
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
deb3b22
.
4526f6b
Add social signin with, Google, Facebook
3b331c5
Merge branch 'master' into feature/social
4750563
Merge branch 'master' of https://github.com/jhipster/generator-jhipst…
56c77d2
delete Facebook key because not working
a5f5d05
fix english social.json by default
9479161
add space on import and remove final key word
d86a0ea
remove font-awesome
26c6426
change position of the question
01c26f3
Revert "remove font-awesome"
3b3a9a8
Merge branch 'master' of https://github.com/moifort/generator-jhipste…
3d71ee9
remove font-awesome and bootsrtap-social
0dab5e1
Merge branch 'master' of https://github.com/jhipster/generator-jhipst…
7fbae52
Add error message when no keys for social connection
d471568
Merge branch 'master' of https://github.com/jhipster/generator-jhipst…
91de8e5
Add gradle dependencies
1e80d28
add style in css with test
e6145ab
Compatibility with DB Table naming
12f4161
Merge branch 'master' of https://github.com/jhipster/generator-jhipst…
29bd005
Add missing packageName
00bd03a
Add test (From Spring) for ConnectionRepository
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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
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
44 changes: 44 additions & 0 deletions
44
app/templates/src/main/java/package/social/_SocialController.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,44 @@ | ||
package <%=packageName%>.social; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.web.ProviderSignInUtils; | ||
import org.springframework.social.support.URIBuilder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.CookieValue; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
import javax.inject.Inject; | ||
|
||
@Controller | ||
@RequestMapping("/social") | ||
public class SocialController { | ||
private final Logger log = LoggerFactory.getLogger(SocialController.class); | ||
|
||
@Inject | ||
private SocialService socialService; | ||
|
||
@Inject | ||
private ProviderSignInUtils providerSignInUtils; | ||
|
||
@RequestMapping(value = "/signup", method = RequestMethod.GET) | ||
public RedirectView signUp(WebRequest webRequest, @CookieValue("NG_TRANSLATE_LANG_KEY") String langKey) { | ||
try { | ||
Connection<?> connection = providerSignInUtils.getConnectionFromSession(webRequest); | ||
socialService.createSocialUser(connection, langKey.replace("\"", "")); | ||
return new RedirectView(URIBuilder.fromUri("/#/social-register/" + connection.getKey().getProviderId()) | ||
.queryParam("success", "true") | ||
.build().toString(), true); | ||
} catch (Exception e) { | ||
log.error("Exception creating social user: ", e); | ||
return new RedirectView(URIBuilder.fromUri("/#/social-register/no-provider") | ||
.queryParam("success", "false") | ||
.build().toString(), true); | ||
} | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
app/templates/src/main/java/package/social/_SocialService.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,110 @@ | ||
package <%=packageName%>.social; | ||
|
||
import <%=packageName%>.domain.Authority; | ||
import <%=packageName%>.domain.User; | ||
import <%=packageName%>.repository.AuthorityRepository; | ||
import <%=packageName%>.repository.UserRepository; | ||
import <%=packageName%>.service.MailService; | ||
|
||
import org.apache.commons.lang.RandomStringUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.social.connect.Connection; | ||
import org.springframework.social.connect.ConnectionRepository; | ||
import org.springframework.social.connect.UserProfile; | ||
import org.springframework.social.connect.UsersConnectionRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.inject.Inject; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
@Service | ||
public class SocialService { | ||
private final Logger log = LoggerFactory.getLogger(SocialService.class); | ||
|
||
@Inject | ||
private UsersConnectionRepository usersConnectionRepository; | ||
|
||
@Inject | ||
private AuthorityRepository authorityRepository; | ||
|
||
@Inject | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@Inject | ||
private UserRepository userRepository; | ||
|
||
@Inject | ||
private MailService mailService; | ||
|
||
public void createSocialUser(Connection<?> connection, String langKey) { | ||
if (connection == null) { | ||
log.error("Cannot create social user because connection is null"); | ||
throw new IllegalArgumentException("Connection cannot be null"); | ||
} | ||
UserProfile userProfile = connection.fetchUserProfile(); | ||
String providerId = connection.getKey().getProviderId(); | ||
User user = createUserIfNotExist(userProfile, langKey, providerId); | ||
createSocialConnection(user.getLogin(), connection); | ||
mailService.sendSocialRegistrationValidationEmail(user, providerId); | ||
} | ||
|
||
private User createUserIfNotExist(UserProfile userProfile, String langKey, String providerId) { | ||
String email = userProfile.getEmail(); | ||
String userName = userProfile.getUsername(); | ||
if (StringUtils.isBlank(email) && StringUtils.isBlank(userName)) { | ||
log.error("Cannot create social user because email and login are null"); | ||
throw new IllegalArgumentException("Email and login cannot be null"); | ||
} | ||
if (StringUtils.isBlank(email) && userRepository.findOneByLogin(userName).isPresent()) { | ||
log.error("Cannot create social user because email is null and login already exist, login -> {}", userName); | ||
throw new IllegalArgumentException("Email cannot be null with an existing login"); | ||
} | ||
Optional<User> user = userRepository.findOneByEmail(email); | ||
if (user.isPresent()) { | ||
log.info("User already exist associate the connection to this account"); | ||
return user.get(); | ||
} | ||
|
||
String login = getLoginDependingOnProviderId(userProfile, providerId); | ||
String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10)); | ||
Set<Authority> authorities = new HashSet<>(1); | ||
authorities.add(authorityRepository.findOne("ROLE_USER")); | ||
|
||
User newUser = new User(); | ||
newUser.setLogin(login); | ||
newUser.setPassword(encryptedPassword); | ||
newUser.setFirstName(userProfile.getFirstName()); | ||
newUser.setLastName(userProfile.getLastName()); | ||
newUser.setEmail(email); | ||
newUser.setActivated(true); | ||
newUser.setAuthorities(authorities); | ||
newUser.setLangKey(langKey); | ||
|
||
return userRepository.save(newUser); | ||
} | ||
|
||
/** | ||
* @param userProfile | ||
* @param providerId | ||
* @return login if provider manage a login like Twitter or Github otherwise email address. | ||
* Because provider like Google or Facebook didn't provide login or login like "12099388847393" | ||
*/ | ||
private String getLoginDependingOnProviderId(UserProfile userProfile, String providerId) { | ||
switch (providerId) { | ||
case "twitter": | ||
return userProfile.getUsername(); | ||
default: | ||
return userProfile.getEmail(); | ||
} | ||
} | ||
|
||
private void createSocialConnection(String login, Connection<?> connection) { | ||
ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(login); | ||
connectionRepository.addConnection(connection); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually (not always perfect) have a space between our package name and the other packages