From fc6b8340b478e4fce7f58250114bf8c90a7c7d68 Mon Sep 17 00:00:00 2001 From: Banghua Zhao Date: Thu, 21 Nov 2024 22:07:12 +1300 Subject: [PATCH] google signin fix --- android/app/build.gradle | 2 +- .../settings/viewModels/login_view_model.dart | 67 ++++++++++++------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index e1298cd..7b7cc27 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -94,7 +94,7 @@ flutter { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // Import the Firebase BoM - implementation platform('com.google.firebase:firebase-bom:33.5.1') + implementation platform('com.google.firebase:firebase-bom:33.6.0') implementation 'com.google.firebase:firebase-auth' // Add the dependencies for Firebase products you want to use diff --git a/lib/presentation/settings/viewModels/login_view_model.dart b/lib/presentation/settings/viewModels/login_view_model.dart index 7c41df9..e0a6f96 100644 --- a/lib/presentation/settings/viewModels/login_view_model.dart +++ b/lib/presentation/settings/viewModels/login_view_model.dart @@ -36,7 +36,8 @@ class LoginViewModel extends ChangeNotifier { void updateButtonState(String email, String password) { final isEmailValid = RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(email); - _isButtonEnabled = isEmailValid && password.isNotEmpty && password.length >= 6; + _isButtonEnabled = + isEmailValid && password.isNotEmpty && password.length >= 6; notifyListeners(); } @@ -57,20 +58,8 @@ class LoginViewModel extends ChangeNotifier { } } - static String GOOGLE_SIGNIN_CLIENT_ID_WEB = dotenv.env['GOOGLE_SIGNIN_CLIENT_ID_WEB'] ?? ""; - static String GOOGLE_SIGNIN_CLIENT_ID_IOS = dotenv.env['GOOGLE_SIGNIN_CLIENT_ID_IOS'] ?? ""; - static String GOOGLE_SIGNIN_CLIENT_ID_AND = dotenv.env['GOOGLE_SIGNIN_CLIENT_ID_AND'] ?? ""; - - final GoogleSignIn _googleSignIn = GoogleSignIn( - clientId: kIsWeb - ? GOOGLE_SIGNIN_CLIENT_ID_WEB - : Platform.isIOS - ? GOOGLE_SIGNIN_CLIENT_ID_IOS - : GOOGLE_SIGNIN_CLIENT_ID_AND, - scopes: [ - 'email', - ], - ); + static String GOOGLE_SIGNIN_CLIENT_ID_WEB = + dotenv.env['GOOGLE_SIGNIN_CLIENT_ID_WEB'] ?? ""; bool _isSigningIn = false; @@ -86,7 +75,18 @@ class LoginViewModel extends ChangeNotifier { notifyListeners(); try { - _user = await _googleSignIn.signIn(); + final GoogleSignIn googleSignIn; + if (kIsWeb) { + googleSignIn = GoogleSignIn( + clientId: GOOGLE_SIGNIN_CLIENT_ID_WEB, + scopes: ['email'], + ); + } else { + googleSignIn = GoogleSignIn( + scopes: ['email'], + ); + } + _user = await googleSignIn.signIn(); if (_user != null) { print('Logged in with Google: ${_user!.email}'); await syncUser(_user!.displayName, _user!.email, _user!.photoUrl); @@ -104,13 +104,27 @@ class LoginViewModel extends ChangeNotifier { // Function to handle Google Sign-Out Future signOut() async { - await _googleSignIn.signOut(); + final GoogleSignIn googleSignIn; + if (kIsWeb) { + googleSignIn = GoogleSignIn( + clientId: GOOGLE_SIGNIN_CLIENT_ID_WEB, + scopes: ['email'], + ); + } else { + googleSignIn = GoogleSignIn( + scopes: ['email'], + ); + } + + await googleSignIn.signOut(); _user = null; notifyListeners(); } - Future syncUser(String? displayName, String email, String? photoUrl) async { - final accessToken = await authUseCase.syncUser(displayName, email, photoUrl); + Future syncUser( + String? displayName, String email, String? photoUrl) async { + final accessToken = + await authUseCase.syncUser(displayName, email, photoUrl); return accessToken; } @@ -124,11 +138,12 @@ class LoginViewModel extends ChangeNotifier { ], webAuthenticationOptions: WebAuthenticationOptions( clientId: 'com.example.swiftcompsignin', - redirectUri: kIsWeb //This is where Apple sends the user back after they sign in. - ? Uri.parse('https://compositesai.com/') - : Uri.parse( - 'https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple', - ), + redirectUri: + kIsWeb //This is where Apple sends the user back after they sign in. + ? Uri.parse('https://compositesai.com/') + : Uri.parse( + 'https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple', + ), ), ); @@ -139,7 +154,8 @@ class LoginViewModel extends ChangeNotifier { } // Send token to backend for validation // This is where app sends token to backend to check if the the "identityToken" is real and safe to use. - final validateTokenEndpoint = Uri.parse('http://localhost:8080/api/auth/sign_in_with_apple'); + final validateTokenEndpoint = + Uri.parse('http://localhost:8080/api/auth/sign_in_with_apple'); final validationResponse = await http.Client().post( validateTokenEndpoint, headers: { @@ -174,5 +190,4 @@ class LoginViewModel extends ChangeNotifier { rethrow; // Optionally rethrow for higher-level error handling } } - }