forked from makersacademy/acebook-java-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from The-Socialites/events
Events
- Loading branch information
Showing
21 changed files
with
279 additions
and
48 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -138,4 +138,4 @@ | |
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
</project> |
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/makersacademy/acebook/controller/LogoutController.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,14 @@ | ||
package com.makersacademy.acebook.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.servlet.view.RedirectView; | ||
|
||
@Controller | ||
public class LogoutController { | ||
@PostMapping("/logout") | ||
public RedirectView logout() { | ||
return new RedirectView("/login"); | ||
} | ||
} |
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
124 changes: 124 additions & 0 deletions
124
src/main/java/com/makersacademy/acebook/service/CustomOAuth2UserService.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,124 @@ | ||
/* | ||
package com.makersacademy.acebook.service; | ||
import com.makersacademy.acebook.model.User; | ||
import com.makersacademy.acebook.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import javax.transaction.Transactional; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
@Service | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
private static final Logger logger = LoggerFactory.getLogger(CustomOAuth2UserService.class); | ||
@Autowired | ||
private UserRepository userRepository; | ||
@Override | ||
@Transactional | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2User oAuth2User = super.loadUser(userRequest); | ||
Map<String, Object> attributes = oAuth2User.getAttributes(); | ||
String email = (String) attributes.get("email"); | ||
logger.info("OAuth2 user attributes: {}", attributes); | ||
if (email == null) { | ||
logger.error("Email not found from OAuth2 provider"); | ||
throw new OAuth2AuthenticationException("Email not found from OAuth2 provider"); | ||
} | ||
// Save or update user information in the database | ||
User user = userRepository.findByEmail(email); | ||
if (user == null) { | ||
logger.info("Creating new user with email: {}", email); | ||
user = new User(); | ||
user.setEmail(email); | ||
user.setUsername((String) attributes.get("name")); | ||
user.setProfilePictureUrl((String) attributes.get("picture")); | ||
userRepository.save(user); | ||
logger.info("New user created with email: {}", email); | ||
} else { | ||
logger.info("Updating existing user with email: {}", email); | ||
user.setUsername((String) attributes.get("name")); | ||
user.setProfilePictureUrl((String) attributes.get("picture")); | ||
userRepository.save(user); | ||
logger.info("Existing user updated with email: {}", email); | ||
} | ||
Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); | ||
mappedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER")); | ||
return new DefaultOAuth2User(mappedAuthorities, oAuth2User.getAttributes(), "email"); | ||
} | ||
} | ||
*/ | ||
|
||
package com.makersacademy.acebook.service; | ||
|
||
import com.makersacademy.acebook.model.User; | ||
import com.makersacademy.acebook.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
@Service | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
OAuth2User oAuth2User = super.loadUser(userRequest); | ||
|
||
Map<String, Object> attributes = oAuth2User.getAttributes(); | ||
String email = (String) attributes.get("email"); | ||
|
||
// Save or update user information in the database | ||
User user = userRepository.findByEmail(email); | ||
if (user == null) { | ||
user = new User(); | ||
user.setEmail(email); | ||
user.setUsername((String) attributes.get("name")); | ||
user.setProfilePictureUrl((String) attributes.get("picture")); | ||
userRepository.save(user); // Ensure user is saved here | ||
} | ||
|
||
Set<GrantedAuthority> mappedAuthorities = new HashSet<>(); | ||
mappedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER")); | ||
|
||
return new DefaultOAuth2User(mappedAuthorities, oAuth2User.getAttributes(), "name"); | ||
} | ||
} | ||
|
||
|
||
|
||
|
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
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V10__add_location_to_events.sql
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,2 @@ | ||
ALTER TABLE events | ||
ADD COLUMN location VARCHAR(250); |
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V11__add_language_to_users.sql
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,2 @@ | ||
ALTER TABLE users | ||
ADD COLUMN language VARCHAR(250); |
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,2 @@ | ||
ALTER TABLE users | ||
ADD COLUMN city VARCHAR(250); |
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V9__add_end_time_to_events.sql
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,2 @@ | ||
ALTER TABLE events | ||
ADD COLUMN scheduled_end_time VARCHAR(250); |
Oops, something went wrong.