11package com .microservice .authentication .controller ;
22
3- import jakarta .servlet .http .HttpServletRequest ;
3+ import java .io .IOException ;
4+ import java .util .Optional ;
5+
6+ import com .microservice .authentication .common .repository .AuthenticationCommonRepository ;
7+ import jakarta .servlet .http .HttpServletResponse ;
48import lombok .extern .slf4j .Slf4j ;
5- import org . springframework . http . MediaType ;
9+
610import org .springframework .security .core .Authentication ;
7- import org .springframework .security .core .context .SecurityContextHolder ;
811import org .springframework .stereotype .Controller ;
912import org .springframework .web .bind .annotation .GetMapping ;
10- import org .springframework .web .bind .annotation .ResponseBody ;
1113
1214@ Slf4j
1315@ Controller
1416public class AndroidOAuth2Controller {
17+ private final AuthenticationCommonRepository authenticationCommonRepository ;
18+
19+ public AndroidOAuth2Controller (AuthenticationCommonRepository authenticationCommonRepository ) {
20+ this .authenticationCommonRepository = authenticationCommonRepository ;
21+ }
1522
16- @ GetMapping (value = "/android/oauth2/callback" , produces = MediaType .TEXT_HTML_VALUE )
17- @ ResponseBody
18- public String androidOAuth2Callback (HttpServletRequest request ) {
23+ @ GetMapping (value = "/android/oauth2/callback" )
24+ public void androidOAuth2Callback (Authentication authentication , HttpServletResponse response ) throws IOException {
1925 // After successful OAuth2 authentication with Google,
2026 // Spring Security redirects here with authenticated session
21- // Android app will intercept this URL via intent-filter
22- // and handle the authentication
27+ // Redirect to custom scheme so Android app can intercept
2328
24- Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
2529 String username = "Unknown" ;
26- String sessionId = request . getSession (). getId ( );
30+ Optional < com . microservice . authentication . common . model . Authentication > findById = authenticationCommonRepository . findByEmail ( authentication . getName () );
2731
28- if (auth != null && auth . isAuthenticated ()) {
29- username = auth . getName ();
32+ if (findById . isPresent ()) {
33+ username = findById . get (). getFullName ();
3034 log .info ("Android OAuth2 callback - User authenticated: {}" , username );
3135 }
3236
33- return "<!DOCTYPE html>" +
37+ // Return HTML with multiple redirect methods
38+ response .setContentType ("text/html" );
39+ response .setStatus (HttpServletResponse .SC_OK );
40+
41+ String html = "<!DOCTYPE html>" +
3442 "<html>" +
3543 "<head>" +
3644 " <meta charset=\" UTF-8\" >" +
3745 " <meta name=\" viewport\" content=\" width=device-width, initial-scale=1.0\" >" +
38- " <meta http-equiv=\" refresh\" content=\" 0;url=spendingbetter://oauth2callback\" >" +
3946 " <title>Authentication Successful</title>" +
4047 " <style>" +
4148 " body {" +
@@ -52,21 +59,36 @@ public String androidOAuth2Callback(HttpServletRequest request) {
5259 " .success-icon { font-size: 4rem; margin-bottom: 1rem; }" +
5360 " h1 { margin: 0 0 1rem 0; font-size: 2rem; }" +
5461 " p { margin: 0; font-size: 1.1rem; opacity: 0.9; }" +
62+ " .button {" +
63+ " display: inline-block;" +
64+ " margin-top: 2rem;" +
65+ " padding: 1rem 2rem;" +
66+ " background: rgba(255,255,255,0.2);" +
67+ " color: white;" +
68+ " text-decoration: none;" +
69+ " border-radius: 8px;" +
70+ " font-size: 1.1rem;" +
71+ " }" +
5572 " </style>" +
5673 " <script>" +
57- " // Immediately redirect to the app using custom scheme" +
58- " window.location.href = 'spendingbetter://oauth2callback';" +
74+ " // Try to redirect after a short delay to ensure page loads" +
75+ " setTimeout(function() {" +
76+ " window.location.replace('spendingbetter://oauth2callback');" +
77+ " }, 100);" +
5978 " </script>" +
6079 "</head>" +
6180 "<body>" +
6281 " <div class=\" container\" >" +
6382 " <div class=\" success-icon\" >✓</div>" +
6483 " <h1>Authentication Successful</h1>" +
65- " <p>Redirecting to app... </p>" +
66- " <p style =\" margin-top: 1rem; opacity: 0.7; font-size: 0.9rem; \" >Welcome, " + username + "</p >" +
67- " <p style=\" margin-top: 2rem; font-size: 0.9rem;\" >If you're not redirected, <a href= \" spendingbetter://oauth2callback \" style= \" color: white; text-decoration: underline; \" >click here</a> </p>" +
84+ " <p>Welcome, " + username + "! </p>" +
85+ " <a href =\" spendingbetter://oauth2callback \" class= \" button \" >Return to App</a >" +
86+ " <p style=\" margin-top: 2rem; font-size: 0.9rem; opacity: 0.7; \" >Please click the button above to return to the app </p>" +
6887 " </div>" +
6988 "</body>" +
7089 "</html>" ;
90+
91+ response .getWriter ().write (html );
92+ response .getWriter ().flush ();
7193 }
7294}
0 commit comments