Skip to content
This repository was archived by the owner on Feb 21, 2021. It is now read-only.

Commit 50514cd

Browse files
authored
Improve the Google auth. (#3)
1 parent 5663923 commit 50514cd

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 0.2.0
2+
3+
- Fixed a bug that flashed signed off content before loading.
4+
- Changed the default behavior of Google sign-in to prompt for account:
5+
6+
```dart
7+
abstract class FirebaseAuth {
8+
Future<FirebaseUser> googleSignIn({bool prompt: true});
9+
}
10+
```
11+
112
## 0.1.1
213

314
- Removed a `print` statement that was always occurring.

lib/src/directives/firebase_auth.dart

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,53 @@ abstract class FirebaseAuth {
3333
Stream<FirebaseUser> currentUser();
3434

3535
/// Returns a future that completes after authenticated.
36-
Future<FirebaseUser> googleSignIn();
36+
///
37+
/// May optionally disable [prompt] if the user is already authenticated.
38+
Future<FirebaseUser> googleSignIn({bool prompt: true});
3739

3840
/// Sign out of any authenticated account.
3941
Future<Null> signOut();
4042
}
4143

4244
class _SdkFirebaseAuth implements FirebaseAuth {
43-
static final sdk.AuthProvider _googleAuth = new sdk.GoogleAuthProvider();
45+
static final _googleAuth = new sdk.GoogleAuthProvider();
4446

4547
final sdk.App _app;
4648
final _onUserChanged = new StreamController<FirebaseUser>.broadcast();
4749

4850
FirebaseUser _currentUser;
51+
bool _wasInitialized = false;
4952

5053
_SdkFirebaseAuth(this._app) {
5154
_app.auth().onAuthStateChanged.listen((event) {
5255
final user = event.user;
56+
_wasInitialized = true;
5357
_currentUser = user != null ? new FirebaseUser._fromSdk(user) : null;
5458
_onUserChanged.add(_currentUser);
5559
});
5660
}
5761

5862
@override
5963
Stream<FirebaseUser> currentUser() async* {
60-
yield _currentUser;
64+
if (_wasInitialized) {
65+
yield _currentUser;
66+
}
6167
yield* _onUserChanged.stream;
6268
}
6369

6470
@override
65-
Future<FirebaseUser> googleSignIn() async {
66-
final user = await _app.auth().signInWithPopup(_googleAuth);
67-
return new FirebaseUser._fromSdk(user.user);
71+
Future<FirebaseUser> googleSignIn({
72+
bool prompt: true,
73+
}) async {
74+
_googleAuth.setCustomParameters(<String, String>{
75+
'prompt': prompt ? 'select_account' : 'none',
76+
});
77+
try {
78+
final user = await _app.auth().signInWithPopup(_googleAuth);
79+
return new FirebaseUser._fromSdk(user.user);
80+
} catch (_) {
81+
return null;
82+
}
6883
}
6984

7085
@override
@@ -132,6 +147,7 @@ class IfFirebaseAuthDirective implements OnDestroy, OnInit {
132147
bool _lastCondition;
133148
FirebaseUser _currentUser;
134149
StreamSubscription<FirebaseUser> _userSub;
150+
bool _wasInitialized = false;
135151

136152
IfFirebaseAuthDirective(
137153
this._authService,
@@ -142,7 +158,9 @@ class IfFirebaseAuthDirective implements OnDestroy, OnInit {
142158
@Input()
143159
set ifFirebaseAuth(bool newCondition) {
144160
_checkCondition = newCondition;
145-
_toggle(_checkCondition ? _currentUser != null : _currentUser == null);
161+
if (_wasInitialized) {
162+
_toggle(_checkCondition ? _currentUser != null : _currentUser == null);
163+
}
146164
}
147165

148166
@override
@@ -155,6 +173,7 @@ class IfFirebaseAuthDirective implements OnDestroy, OnInit {
155173
_userSub = _authService.currentUser().listen((user) {
156174
_currentUser = user;
157175
_toggle(_checkCondition ? _currentUser != null : _currentUser == null);
176+
_wasInitialized = true;
158177
});
159178
}
160179

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: angular_fire
22
description: >
33
Unofficial library for AngularDart and Firebase.
4-
version: 0.1.1
4+
version: 0.2.0
55
authors:
66
- Matan Lurey <matanl@google.com>
77
homepage: https://github.com/matanlurey/angular_fire

0 commit comments

Comments
 (0)