diff --git a/src/main/java/com/swp/PodBookingSystem/configuration/SecurityConfig.java b/src/main/java/com/swp/PodBookingSystem/configuration/SecurityConfig.java index 31ba35a5..f719a6fa 100644 --- a/src/main/java/com/swp/PodBookingSystem/configuration/SecurityConfig.java +++ b/src/main/java/com/swp/PodBookingSystem/configuration/SecurityConfig.java @@ -50,6 +50,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti request.requestMatchers(PUBLIC_ENDPOINTS).permitAll() .anyRequest().permitAll()) .oauth2Login(oauth2login -> oauth2login.loginPage("http://localhost:3000/login") + .loginPage("https://flexipod.site/login") .successHandler(((request, response, authentication) -> response.sendRedirect("/auth/login/google")))) ; diff --git a/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java b/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java index beac5e2a..66ca09ab 100644 --- a/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java +++ b/src/main/java/com/swp/PodBookingSystem/controller/AuthenticationController.java @@ -42,17 +42,38 @@ public class AuthenticationController { @Value("${google.failure}") protected String urlFailure; - @GetMapping("/login/google") - public RedirectView loginGoogle(OAuth2AuthenticationToken token) throws ParseException, UnsupportedEncodingException { +// @GetMapping("/login/google") +// public RedirectView loginGoogle(OAuth2AuthenticationToken token) throws ParseException, UnsupportedEncodingException { +// try { +// var result = authenticationService.loginGoogle(token.getPrincipal().getAttribute("email"), +// token.getPrincipal().getAttribute("name"), +// token.getPrincipal().getAttribute("picture")); +// return new RedirectView(urlSuccess + result.getAccessToken() +// + "&refreshToken=" + result.getRefreshToken() + "&status=" + 200); +// } catch (Exception e) { +// String message = URLEncoder.encode("Tài khoản đã bị cấm", "UTF-8"); +// return new RedirectView(urlFailure + message + "&status=" + 500); +// } +// } + + @PostMapping("/login/google") + ApiResponse loginGoogle(@RequestBody FirebaseToken token) throws ParseException, UnsupportedEncodingException { try { - var result = authenticationService.loginGoogle(token.getPrincipal().getAttribute("email"), - token.getPrincipal().getAttribute("name"), - token.getPrincipal().getAttribute("picture")); - return new RedirectView(urlSuccess + result.getAccessToken() - + "&refreshToken=" + result.getRefreshToken() + "&status=" + 200); + log.info("email:" + token.getEmail()); + var result = authenticationService.loginGoogle(token.getEmail(), + token.getName(), + token.getAvatar()); + String redirectUrl = urlSuccess + result.getAccessToken() + + "&refreshToken=" + result.getRefreshToken() + "&status=" + 200; + return ApiResponse.builder() + .data(new LoginGoogleRes(redirectUrl)) + .build(); } catch (Exception e) { String message = URLEncoder.encode("Tài khoản đã bị cấm", "UTF-8"); - return new RedirectView(urlFailure + message + "&status=" + 500); + String errorUrl = urlFailure + message + "&status=" + 500; + return ApiResponse.builder() + .data(new LoginGoogleRes(errorUrl)) + .build(); } } diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/FirebaseToken.java b/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/FirebaseToken.java new file mode 100644 index 00000000..586b2916 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/FirebaseToken.java @@ -0,0 +1,15 @@ +package com.swp.PodBookingSystem.dto.request.Authentication; + +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@FieldDefaults(level = AccessLevel.PRIVATE) +public class FirebaseToken { + String email; + String name; + String avatar; +} diff --git a/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/LoginGoogleRes.java b/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/LoginGoogleRes.java new file mode 100644 index 00000000..d02ea460 --- /dev/null +++ b/src/main/java/com/swp/PodBookingSystem/dto/request/Authentication/LoginGoogleRes.java @@ -0,0 +1,13 @@ +package com.swp.PodBookingSystem.dto.request.Authentication; + +import lombok.*; +import lombok.experimental.FieldDefaults; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@FieldDefaults(level = AccessLevel.PRIVATE) +public class LoginGoogleRes { + String url; +}