Skip to content

Commit

Permalink
fix: minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ngntu10 committed Oct 25, 2024
1 parent 4a84c23 commit 5c26036
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
16 changes: 8 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM openjdk:17.0.2-oraclelinux8
# Working directory
WORKDIR /app
# Copy from your host(PC, laptop) to container
COPY . /app
RUN ./mvnw dependency:go-offline
# RUN ./mvnw package
CMD ["./mvnw", "spring-boot:run"]
EXPOSE 8080
## Working directory
#WORKDIR /app
## Copy from your host(PC, laptop) to container
#COPY . /app
#RUN ./mvnw dependency:go-offline
## RUN ./mvnw package
#CMD ["./mvnw", "spring-boot:run"]
#EXPOSE 8080
2 changes: 2 additions & 0 deletions src/main/java/com/Optimart/constants/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static final class Auth {
public static final String LOGIN = "/login";
public static final String LOGIN_GOOGLE = "/login-google";
public static final String REGISTER_GOOGLE ="/register-google";
public static final String LOGIN_FACEBOOK = "/login-facebook";
public static final String REGISTER_FACEBOOK ="/register-facebook";
public static final String ME = "/me";
public static final String REGISTER = "/register";
public static final String REFRESH_TOKEN = "/refreshtoken";
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/Optimart/controllers/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ public ResponseEntity<?> registerGoogle(@RequestBody OAuth2DTO oAuth2DTO){
}
}

@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = LoginResponse.class), mediaType = "application/json"))
@UnsecuredSwaggerOperation(summary = "Register user by Facebook")
@PostMapping(Endpoint.Auth.REGISTER_FACEBOOK)
public ResponseEntity<?> registerFacebook(@RequestBody OAuth2DTO oAuth2DTO){
try {
User registerUser = authService.registerGoogle(oAuth2DTO.getIdToken());
return ResponseEntity.ok(new RegisterResponse(localizationUtils.getLocalizedMessage(MessageKeys.REGISTER_SUCCESSFULLY), registerUser));
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RegisterResponse(localizationUtils.getLocalizedMessage(MessageKeys.REGISTER_FAILED, ex.getMessage()), null));
}
}

@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = LoginResponse.class), mediaType = "application/json"))
@UnsecuredSwaggerOperation(summary = "Login user by Facebook")
@PostMapping(Endpoint.Auth.LOGIN_FACEBOOK)
public ResponseEntity<?> loginFacebook(@RequestBody OAuth2DTO oAuth2DTO){
try {
GoogleUserInfoResponse googleUserInfoResponse = googleService.getUserInfo(oAuth2DTO.getIdToken());
String access_token = authService.loginGoogle(oAuth2DTO.getIdToken());
RefreshToken refreshToken = refreshTokenService.createRefreshToken(googleUserInfoResponse.getEmail());
User user = authService.getUserInfo(googleUserInfoResponse.getEmail());
UserLoginResponse userLoginResponse = mapper.map(user, UserLoginResponse.class);
userLoginResponse.setCity(user.getCity());
return ResponseEntity.ok(LoginResponse.success(localizationUtils.getLocalizedMessage(MessageKeys.LOGIN_SUCCESSFULLY),
access_token, refreshToken.getRefreshtoken(), userLoginResponse));
} catch (Exception e) {
return ResponseEntity.badRequest().body(LoginResponse.failure(e.getMessage()));
}
}

@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = UserLoginResponse.class), mediaType = "application/json"))
@SecuredSwaggerOperation(summary = "Get my info user")
@GetMapping (Endpoint.Auth.ME)
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/Optimart/services/Auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,26 @@ public User registerGoogle(String token) {
public String loginGoogle(String token) throws Exception {
GoogleUserInfoResponse googleUserInfoResponse = googleService.getUserInfo(token);
Optional<User> optionalUser = authRepository.findByGoogleAccountId(googleUserInfoResponse.getSub());
if(optionalUser.isEmpty()) throw new DataNotFoundException(localizationUtils.getLocalizedMessage(MessageKeys.USER_NOT_EXIST));
User user = optionalUser.get();
return jwtTokenUtil.generateToken(user);
if(optionalUser.isEmpty()){
Role userRole = roleRepository.findByName("BASIC")
.orElseThrow(() -> new DataNotFoundException(localizationUtils.getLocalizedMessage(MessageKeys.ERROR)));;
User newUser = User.builder()
.role(userRole)
.email(googleUserInfoResponse.getEmail())
.googleAccountId(googleUserInfoResponse.getSub())
.status(1)
.userType(1) // 1: Google, 2: Facebook, 3: email
.fullName(googleUserInfoResponse.getName())
.userName(googleUserInfoResponse.getName())
.imageUrl(googleUserInfoResponse.getPicture())
.build();
authRepository.save(newUser);
return jwtTokenUtil.generateToken(newUser);
}
else {
User user = optionalUser.get();
return jwtTokenUtil.generateToken(user);
}
}

@Transactional
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ spring.security.oauth2.client.registration.google.client-id = ${GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret = ${GOOGLE_SECRET}
spring.security.oauth2.client.registration.google.scope = email.profile

### META CLOUD OAUTH 2.0
spring.security.oauth2.client.registration.facebook.client-id = ${FACEBOOK_CLIENT_ID}
spring.security.oauth2.client.registration.facebook.client-secret = ${FACEBOOK_SECRET}
spring.security.oauth2.client.registration.facebook.scope = email.profile

0 comments on commit 5c26036

Please sign in to comment.