|
1 | 1 | import 'package:firebase_auth/firebase_auth.dart'; |
2 | 2 | import 'package:flutter/foundation.dart'; |
3 | 3 | import 'package:google_sign_in/google_sign_in.dart'; |
4 | | -import 'package:connectivity_plus/connectivity_plus.dart'; |
5 | 4 |
|
6 | 5 | class AuthService { |
7 | 6 | final FirebaseAuth _auth = FirebaseAuth.instance; |
8 | | - final GoogleSignIn _googleSignIn = GoogleSignIn(); |
| 7 | + final GoogleSignIn _googleSignIn = GoogleSignIn.instance; |
| 8 | + bool _isGoogleSignInInitialized = false; |
9 | 9 |
|
10 | | - // Função para autenticação com Google |
11 | | - Future<User?> signInWithGoogle() async { |
| 10 | + Future<void> _initializeGoogleSignIn() async { |
12 | 11 | try { |
13 | | - // Verificação de conectividade |
14 | | - final connectivityResults = await Connectivity().checkConnectivity(); |
15 | | - if (connectivityResults.contains(ConnectivityResult.none)) { |
16 | | - if (kDebugMode) print("Sem conexão com a internet"); |
17 | | - return null; |
| 12 | + await _googleSignIn.initialize(); |
| 13 | + _isGoogleSignInInitialized = true; |
| 14 | + } catch (e) { |
| 15 | + if (kDebugMode) { |
| 16 | + print('Falha ao inicializar o Google Sign-In: $e'); |
18 | 17 | } |
| 18 | + } |
| 19 | + } |
19 | 20 |
|
20 | | - // Realizar login com Google |
21 | | - final GoogleSignInAccount? googleSignInAccount = await _googleSignIn |
22 | | - .signIn(); |
23 | | - if (googleSignInAccount == null) { |
24 | | - if (kDebugMode) print("Login cancelado pelo usuário."); |
25 | | - return null; |
26 | | - } |
| 21 | + Future<void> _ensureGoogleSignInInitialized() async { |
| 22 | + if (!_isGoogleSignInInitialized) { |
| 23 | + await _initializeGoogleSignIn(); |
| 24 | + } |
| 25 | + } |
27 | 26 |
|
28 | | - // Autenticação no Firebase com as credenciais do Google |
29 | | - final GoogleSignInAuthentication googleAuth = |
30 | | - await googleSignInAccount.authentication; |
31 | | - final AuthCredential credential = GoogleAuthProvider.credential( |
32 | | - accessToken: googleAuth.accessToken, |
33 | | - idToken: googleAuth.idToken, |
34 | | - ); |
| 27 | + // Função para autenticação com Google |
| 28 | + Future<UserCredential?> signInWithGoogle() async { |
| 29 | + await _ensureGoogleSignInInitialized(); |
| 30 | + try { |
| 31 | + final GoogleSignInAccount googleSignInAccount = await _googleSignIn |
| 32 | + .authenticate(); |
35 | 33 |
|
36 | | - final UserCredential authResult = await _auth.signInWithCredential( |
37 | | - credential, |
38 | | - ); |
39 | | - final User? user = authResult.user; |
| 34 | + final GoogleSignInAuthentication googleAuth = |
| 35 | + googleSignInAccount.authentication; |
| 36 | + final idToken = googleAuth.idToken; |
40 | 37 |
|
41 | | - if (user != null) { |
42 | | - if (kDebugMode) { |
43 | | - print('Usuário autenticado com sucesso: ${user.displayName}'); |
44 | | - } |
45 | | - } else { |
46 | | - if (kDebugMode) print('Erro: Autenticação retornou usuário nulo.'); |
| 38 | + if (idToken == null) { |
| 39 | + throw Exception('Não foi possível obter o token de ID do Google.'); |
47 | 40 | } |
48 | 41 |
|
49 | | - return user; |
50 | | - } on FirebaseAuthException catch (e) { |
51 | | - if (kDebugMode) print('Erro do Firebase na autenticação: ${e.message}'); |
52 | | - return null; |
53 | | - } on Exception catch (e) { |
54 | | - if (kDebugMode) print('Erro desconhecido na autenticação: $e'); |
55 | | - return null; |
| 42 | + final AuthCredential credential = GoogleAuthProvider.credential( |
| 43 | + accessToken: googleAuth.idToken, |
| 44 | + idToken: idToken, |
| 45 | + ); |
| 46 | + |
| 47 | + return await _auth.signInWithCredential(credential); |
| 48 | + } catch (e) { |
| 49 | + if (kDebugMode) print('Erro na autenticação com Google: $e'); |
| 50 | + await signOut(); |
| 51 | + rethrow; |
56 | 52 | } |
57 | 53 | } |
58 | 54 |
|
|
0 commit comments