Skip to content

Commit e6f197b

Browse files
committed
Update firebase_notifier.dart
1 parent f95268d commit e6f197b

File tree

8 files changed

+522
-114
lines changed

8 files changed

+522
-114
lines changed

in_app_purchases/codelab_rebuild.yaml

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16126,19 +16126,31 @@ steps:
1612616126
import 'package:flutter/cupertino.dart';
1612716127
import 'package:flutter/foundation.dart';
1612816128
import 'package:google_sign_in/google_sign_in.dart';
16129+
import 'package:logging/logging.dart';
1612916130

1613016131
import '../firebase_options.dart';
1613116132
import '../model/firebase_state.dart';
1613216133

16134+
final _log = Logger('FirebaseNotifier');
16135+
1613316136
class FirebaseNotifier extends ChangeNotifier {
1613416137
bool loggedIn = false;
1613516138
FirebaseState state = FirebaseState.loading;
1613616139
bool isLoggingIn = false;
16140+
late final GoogleSignIn _googleSignIn;
16141+
StreamSubscription<User?>? _authSubscription;
1613716142

1613816143
FirebaseNotifier() {
16144+
_googleSignIn = GoogleSignIn.instance;
1613916145
load();
1614016146
}
1614116147

16148+
@override
16149+
void dispose() {
16150+
_authSubscription?.cancel();
16151+
super.dispose();
16152+
}
16153+
1614216154
late final Completer<bool> _isInitialized = Completer();
1614316155

1614416156
Future<FirebaseFirestore> get firestore async {
@@ -16150,52 +16162,91 @@ steps:
1615016162
}
1615116163

1615216164
Future<void> load() async {
16165+
_log.info('Starting Firebase initialization');
1615316166
try {
16167+
_log.fine('Initializing Firebase app');
1615416168
await Firebase.initializeApp(
1615516169
options: DefaultFirebaseOptions.currentPlatform,
1615616170
);
16171+
16172+
_log.fine('Initializing Google Sign-In');
16173+
await _googleSignIn.initialize();
16174+
16175+
// Set up auth state listener before attempting authentication
16176+
_log.fine('Setting up auth state listener');
16177+
_authSubscription = FirebaseAuth.instance.authStateChanges().listen((
16178+
user,
16179+
) {
16180+
_log.fine(
16181+
'Auth state changed: user is ${user != null ? 'logged in' : 'logged out'}',
16182+
);
16183+
loggedIn = user != null;
16184+
notifyListeners();
16185+
});
16186+
16187+
// Check if user is already authenticated
16188+
_log.fine('Attempting lightweight authentication');
16189+
final lightweightAuth = _googleSignIn.attemptLightweightAuthentication();
16190+
if (lightweightAuth is Future) {
16191+
await lightweightAuth;
16192+
}
16193+
1615716194
loggedIn = FirebaseAuth.instance.currentUser != null;
1615816195
state = FirebaseState.available;
1615916196
_isInitialized.complete(true);
16197+
_log.info('Firebase initialization completed successfully');
1616016198
notifyListeners();
1616116199
} catch (e) {
16200+
_log.severe('Firebase initialization failed', e);
1616216201
state = FirebaseState.notAvailable;
1616316202
_isInitialized.complete(false);
1616416203
notifyListeners();
1616516204
}
1616616205
}
1616716206

1616816207
Future<void> login() async {
16208+
_log.info('Starting login process');
1616916209
isLoggingIn = true;
1617016210
notifyListeners();
16171-
// Trigger the authentication flow
16211+
1617216212
try {
16173-
final googleUser = await GoogleSignIn().signIn();
16174-
if (googleUser == null) {
16213+
_log.fine('Authenticating with Google Sign-In');
16214+
await _googleSignIn.authenticate();
16215+
16216+
_log.fine('Requesting server authorization');
16217+
final serverAuth = await _googleSignIn.authorizationClient
16218+
.authorizeServer(['email', 'profile']);
16219+
16220+
if (serverAuth == null) {
16221+
_log.warning('Server authorization returned null');
1617516222
isLoggingIn = false;
1617616223
notifyListeners();
1617716224
return;
1617816225
}
1617916226

16180-
// Obtain the auth details from the request
16181-
final googleAuth = await googleUser.authentication;
16182-
16183-
// Create a new credential
16227+
_log.fine('Creating Firebase credential');
1618416228
final credential = GoogleAuthProvider.credential(
16185-
accessToken: googleAuth.accessToken,
16186-
idToken: googleAuth.idToken,
16229+
idToken: serverAuth.serverAuthCode,
1618716230
);
1618816231

16189-
// Once signed in, return the UserCredential
16232+
_log.fine('Signing in to Firebase');
1619016233
await FirebaseAuth.instance.signInWithCredential(credential);
1619116234

16192-
loggedIn = true;
16193-
isLoggingIn = false;
16194-
notifyListeners();
16235+
_log.info('Login completed successfully');
16236+
} on GoogleSignInException catch (e) {
16237+
_log.info('Google Sign-In exception: ${e.code}');
16238+
if (e.code == GoogleSignInExceptionCode.canceled) {
16239+
_log.fine('User canceled sign-in');
16240+
} else {
16241+
_log.warning('Google Sign-In failed', e);
16242+
}
16243+
return;
1619516244
} catch (e) {
16245+
_log.severe('Login failed with unexpected error', e);
16246+
return;
16247+
} finally {
1619616248
isLoggingIn = false;
1619716249
notifyListeners();
16198-
return;
1619916250
}
1620016251
}
1620116252
}
@@ -17616,15 +17667,15 @@ steps:
1761617667
patch-u: |
1761717668
--- b/in_app_purchases/step_10/app/lib/logic/firebase_notifier.dart
1761817669
+++ a/in_app_purchases/step_10/app/lib/logic/firebase_notifier.dart
17619-
@@ -29,6 +29,8 @@ class FirebaseNotifier extends ChangeNotifier {
17670+
@@ -41,6 +41,8 @@ class FirebaseNotifier extends ChangeNotifier {
1762017671
return FirebaseFirestore.instance;
1762117672
}
1762217673

1762317674
+ User? get user => FirebaseAuth.instance.currentUser;
1762417675
+
1762517676
Future<void> load() async {
17677+
_log.info('Starting Firebase initialization');
1762617678
try {
17627-
await Firebase.initializeApp(
1762817679
- name: Patch app/lib/pages/purchase_page.dart
1762917680
path: steps/app/lib/pages/purchase_page.dart
1763017681
patch-u: |

in_app_purchases/complete/app/lib/logic/firebase_notifier.dart

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@ import 'package:firebase_core/firebase_core.dart';
66
import 'package:flutter/cupertino.dart';
77
import 'package:flutter/foundation.dart';
88
import 'package:google_sign_in/google_sign_in.dart';
9+
import 'package:logging/logging.dart';
910

1011
import '../firebase_options.dart';
1112
import '../model/firebase_state.dart';
1213

14+
final _log = Logger('FirebaseNotifier');
15+
1316
class FirebaseNotifier extends ChangeNotifier {
1417
bool loggedIn = false;
1518
FirebaseState state = FirebaseState.loading;
1619
bool isLoggingIn = false;
20+
late final GoogleSignIn _googleSignIn;
21+
StreamSubscription<User?>? _authSubscription;
1722

1823
FirebaseNotifier() {
24+
_googleSignIn = GoogleSignIn.instance;
1925
load();
2026
}
2127

28+
@override
29+
void dispose() {
30+
_authSubscription?.cancel();
31+
super.dispose();
32+
}
33+
2234
late final Completer<bool> _isInitialized = Completer();
2335

2436
Future<FirebaseFirestore> get firestore async {
@@ -32,52 +44,91 @@ class FirebaseNotifier extends ChangeNotifier {
3244
User? get user => FirebaseAuth.instance.currentUser;
3345

3446
Future<void> load() async {
47+
_log.info('Starting Firebase initialization');
3548
try {
49+
_log.fine('Initializing Firebase app');
3650
await Firebase.initializeApp(
3751
options: DefaultFirebaseOptions.currentPlatform,
3852
);
53+
54+
_log.fine('Initializing Google Sign-In');
55+
await _googleSignIn.initialize();
56+
57+
// Set up auth state listener before attempting authentication
58+
_log.fine('Setting up auth state listener');
59+
_authSubscription = FirebaseAuth.instance.authStateChanges().listen((
60+
user,
61+
) {
62+
_log.fine(
63+
'Auth state changed: user is ${user != null ? 'logged in' : 'logged out'}',
64+
);
65+
loggedIn = user != null;
66+
notifyListeners();
67+
});
68+
69+
// Check if user is already authenticated
70+
_log.fine('Attempting lightweight authentication');
71+
final lightweightAuth = _googleSignIn.attemptLightweightAuthentication();
72+
if (lightweightAuth is Future) {
73+
await lightweightAuth;
74+
}
75+
3976
loggedIn = FirebaseAuth.instance.currentUser != null;
4077
state = FirebaseState.available;
4178
_isInitialized.complete(true);
79+
_log.info('Firebase initialization completed successfully');
4280
notifyListeners();
4381
} catch (e) {
82+
_log.severe('Firebase initialization failed', e);
4483
state = FirebaseState.notAvailable;
4584
_isInitialized.complete(false);
4685
notifyListeners();
4786
}
4887
}
4988

5089
Future<void> login() async {
90+
_log.info('Starting login process');
5191
isLoggingIn = true;
5292
notifyListeners();
53-
// Trigger the authentication flow
93+
5494
try {
55-
final googleUser = await GoogleSignIn().signIn();
56-
if (googleUser == null) {
95+
_log.fine('Authenticating with Google Sign-In');
96+
await _googleSignIn.authenticate();
97+
98+
_log.fine('Requesting server authorization');
99+
final serverAuth = await _googleSignIn.authorizationClient
100+
.authorizeServer(['email', 'profile']);
101+
102+
if (serverAuth == null) {
103+
_log.warning('Server authorization returned null');
57104
isLoggingIn = false;
58105
notifyListeners();
59106
return;
60107
}
61108

62-
// Obtain the auth details from the request
63-
final googleAuth = await googleUser.authentication;
64-
65-
// Create a new credential
109+
_log.fine('Creating Firebase credential');
66110
final credential = GoogleAuthProvider.credential(
67-
accessToken: googleAuth.accessToken,
68-
idToken: googleAuth.idToken,
111+
idToken: serverAuth.serverAuthCode,
69112
);
70113

71-
// Once signed in, return the UserCredential
114+
_log.fine('Signing in to Firebase');
72115
await FirebaseAuth.instance.signInWithCredential(credential);
73116

74-
loggedIn = true;
75-
isLoggingIn = false;
76-
notifyListeners();
117+
_log.info('Login completed successfully');
118+
} on GoogleSignInException catch (e) {
119+
_log.info('Google Sign-In exception: ${e.code}');
120+
if (e.code == GoogleSignInExceptionCode.canceled) {
121+
_log.fine('User canceled sign-in');
122+
} else {
123+
_log.warning('Google Sign-In failed', e);
124+
}
125+
return;
77126
} catch (e) {
127+
_log.severe('Login failed with unexpected error', e);
128+
return;
129+
} finally {
78130
isLoggingIn = false;
79131
notifyListeners();
80-
return;
81132
}
82133
}
83134
}

0 commit comments

Comments
 (0)