Skip to content

Commit

Permalink
[NguyenHCP] feat: login google with firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhcp2004 committed Nov 12, 2024
1 parent 2e43143 commit 4850b11
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))))
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginGoogleRes> 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.<LoginGoogleRes>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.<LoginGoogleRes>builder()
.data(new LoginGoogleRes(errorUrl))
.build();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4850b11

Please sign in to comment.