From c5add1094fdec20ab55f5501189a4e3f52bff7c8 Mon Sep 17 00:00:00 2001
From: Muhammad Raihan Akbar <ianakbar711@gmail.com>
Date: Wed, 5 Mar 2025 11:38:05 +0700
Subject: [PATCH] [GREEN] Implement Google Auth Endpoint

---
 .../controller/AuthenticationController.java  | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java b/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java
index 7f03a44..85f65ca 100644
--- a/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java
+++ b/src/main/java/com/safetypin/authentication/controller/AuthenticationController.java
@@ -4,6 +4,8 @@ import com.safetypin.authentication.dto.*;
 import com.safetypin.authentication.exception.InvalidCredentialsException;
 import com.safetypin.authentication.exception.UserAlreadyExistsException;
 import com.safetypin.authentication.service.AuthenticationService;
+import com.safetypin.authentication.service.GoogleAuthService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import jakarta.validation.Valid;
@@ -14,9 +16,12 @@ import org.springframework.web.bind.annotation.*;
 public class AuthenticationController {
 
     private final AuthenticationService authenticationService;
+    private final GoogleAuthService googleAuthService;
 
-    public AuthenticationController(AuthenticationService authenticationService) {
+    @Autowired
+    public AuthenticationController(AuthenticationService authenticationService, GoogleAuthService googleAuthService) {
         this.authenticationService = authenticationService;
+        this.googleAuthService = googleAuthService;
     }
 
 
@@ -86,7 +91,18 @@ public class AuthenticationController {
 
     }
 
-
+    @PostMapping("/google")
+    public ResponseEntity<?> authenticateGoogle(@Valid @RequestBody GoogleAuthDTO googleAuthData) {
+        try {
+            String jwt = googleAuthService.authenticate(googleAuthData);
+            return ResponseEntity.ok(new AuthResponse(true, "OK", new Token(jwt)));
+        } catch (UserAlreadyExistsException e) {
+            AuthResponse response = new AuthResponse(false, e.getMessage(), null);
+            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
+        } catch (Exception e) {
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Authentication failed: " + e.getMessage());
+        }
+    }
 
 
     // Endpoint for forgot password (only for email users)
-- 
GitLab