import 'package:firebase_auth/firebase_auth.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:get/get.dart'; class Google { String? token = ''; String? email = ''; String? displayName = ''; String? id = ''; String? accessToken = ''; // 登录并返回 Future signInWithGoogle() async { try { // 确保用户已经登录,并获取当前用户的身份验证令牌 final currentUser = FirebaseAuth.instance.currentUser; print('Future响应数据: $currentUser'); if (currentUser != null) { // 这里处理您需要使用这个JWT令牌的逻辑,例如将它存储到本地存储中作为凭据。 token != await currentUser.getIdToken(); print('Future响应数据tokenResult: $token'); return true; } else { UserCredential userCredential = await loginGoogle(); if (token != "") { return true; } else { return false; } // logger.d("user login google: ${userCredential.user?.displayName}"); } } catch (e) { print("Google 登录异常:$e"); return false; } } // Google登录 Future loginGoogle() async { print('loginGoogle:'); final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); email = googleUser?.email; id = googleUser?.id; displayName = googleUser?.displayName; print('loginGoogle: $googleUser'); // Obtain the auth details from the request final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication; accessToken = googleAuth?.accessToken; token = googleAuth?.idToken; print('loginGoogle:googleAuth?.idToken= ${token}'); print('loginGoogle:googleAuth?.accessToken= ${accessToken}'); // Create a new credential, final credential = GoogleAuthProvider.credential( accessToken: googleAuth?.accessToken, idToken: googleAuth?.idToken, ); // Once signed in, return the UserCredential return await FirebaseAuth.instance.signInWithCredential(credential); } // 退出Google登录 Future signOutGoogle() async { await FirebaseAuth.instance.signOut(); await GoogleSignIn().signOut(); } }