|
| 1 | +// Copyright 2017, Google Inc. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:angular2/angular2.dart'; |
| 8 | +import 'package:firebase/firebase.dart' as sdk; |
| 9 | + |
| 10 | +/// An injectable service representing authentication with Firebase. |
| 11 | +/// |
| 12 | +/// The default implementation requires providing a [sdk.App] object: |
| 13 | +/// ```dart |
| 14 | +/// import 'package:angular2/angular2.dart'; |
| 15 | +/// import 'package:angular2/platform/browser.dart'; |
| 16 | +/// import 'package:firebase/firebase.dart' as sdk; |
| 17 | +/// |
| 18 | +/// bootstrap(AngularFireExample, [ |
| 19 | +/// provide(FirebaseAuth, useValue: new FirebaseAuth( |
| 20 | +/// sdk.initializeApp( |
| 21 | +/// ... |
| 22 | +/// ), |
| 23 | +/// )), |
| 24 | +/// ]); |
| 25 | +/// ```` |
| 26 | +@Injectable() |
| 27 | +abstract class FirebaseAuth { |
| 28 | + factory FirebaseAuth(sdk.App app) = _SdkFirebaseAuth; |
| 29 | + |
| 30 | + /// Returns a stream of authenticated users. |
| 31 | + /// |
| 32 | + /// A value of `null` should be treated as not signed in. |
| 33 | + Stream<FirebaseUser> currentUser(); |
| 34 | + |
| 35 | + /// Returns a future that completes after authenticated. |
| 36 | + Future<FirebaseUser> googleSignIn(); |
| 37 | + |
| 38 | + /// Sign out of any authenticated account. |
| 39 | + Future<Null> signOut(); |
| 40 | +} |
| 41 | + |
| 42 | +class _SdkFirebaseAuth implements FirebaseAuth { |
| 43 | + static final sdk.AuthProvider _googleAuth = new sdk.GoogleAuthProvider(); |
| 44 | + |
| 45 | + final sdk.App _app; |
| 46 | + final _onUserChanged = new StreamController<FirebaseUser>.broadcast(); |
| 47 | + |
| 48 | + FirebaseUser _currentUser; |
| 49 | + |
| 50 | + _SdkFirebaseAuth(this._app) { |
| 51 | + _app.auth().onAuthStateChanged.listen((event) { |
| 52 | + final user = event.user; |
| 53 | + _currentUser = user != null ? new FirebaseUser._fromSdk(user) : null; |
| 54 | + _onUserChanged.add(_currentUser); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + @override |
| 59 | + Stream<FirebaseUser> currentUser() async* { |
| 60 | + yield _currentUser; |
| 61 | + yield* _onUserChanged.stream; |
| 62 | + } |
| 63 | + |
| 64 | + @override |
| 65 | + Future<FirebaseUser> googleSignIn() async { |
| 66 | + final user = await _app.auth().signInWithPopup(_googleAuth); |
| 67 | + return new FirebaseUser._fromSdk(user.user); |
| 68 | + } |
| 69 | + |
| 70 | + @override |
| 71 | + Future<Null> signOut() async { |
| 72 | + await _app.auth().signOut(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Represents a user that is authenticated with Firebase. |
| 77 | +class FirebaseUser { |
| 78 | + final String displayName; |
| 79 | + final String emailAddress; |
| 80 | + final String userId; |
| 81 | + final String photoUrl; |
| 82 | + final String providerId; |
| 83 | + |
| 84 | + factory FirebaseUser._fromSdk(sdk.User user) { |
| 85 | + return new FirebaseUser( |
| 86 | + displayName: user.displayName, |
| 87 | + emailAddress: user.email, |
| 88 | + userId: user.uid, |
| 89 | + photoUrl: user.photoURL, |
| 90 | + providerId: user.providerId, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + const FirebaseUser({ |
| 95 | + this.displayName, |
| 96 | + this.emailAddress, |
| 97 | + this.userId, |
| 98 | + this.photoUrl, |
| 99 | + this.providerId, |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +/// Conditionally shows content based on authentication status with Firebase. |
| 104 | +/// |
| 105 | +/// ```html |
| 106 | +/// <div *ifFirebaseAuth="true"> |
| 107 | +/// Logged in! |
| 108 | +/// </div> |
| 109 | +/// <div *ifFirebaseAuth="false"> |
| 110 | +/// Logged out! |
| 111 | +/// </div> |
| 112 | +/// ```` |
| 113 | +/// |
| 114 | +/// **NOTE**: Only a static value of "true" or "false" is supported. |
| 115 | +/// |
| 116 | +/// You can get a handle to the logged in user: |
| 117 | +/// |
| 118 | +/// ```html |
| 119 | +/// <div *ifFirebaseAuth="true; let currentUser = currentUser"> |
| 120 | +/// Logged in as {{currentUser.displayName}}! |
| 121 | +/// </div> |
| 122 | +/// ``` |
| 123 | +@Directive( |
| 124 | + selector: '[ifFirebaseAuth]', |
| 125 | +) |
| 126 | +class IfFirebaseAuthDirective implements OnDestroy, OnInit { |
| 127 | + final FirebaseAuth _authService; |
| 128 | + final TemplateRef _templateRef; |
| 129 | + final ViewContainerRef _viewContainerRef; |
| 130 | + |
| 131 | + bool _checkCondition; |
| 132 | + bool _lastCondition; |
| 133 | + FirebaseUser _currentUser; |
| 134 | + StreamSubscription<FirebaseUser> _userSub; |
| 135 | + |
| 136 | + IfFirebaseAuthDirective( |
| 137 | + this._authService, |
| 138 | + this._templateRef, |
| 139 | + this._viewContainerRef, |
| 140 | + ); |
| 141 | + |
| 142 | + @Input() |
| 143 | + set ifFirebaseAuth(bool newCondition) { |
| 144 | + _checkCondition = newCondition; |
| 145 | + _toggle(_checkCondition ? _currentUser != null : _currentUser == null); |
| 146 | + } |
| 147 | + |
| 148 | + @override |
| 149 | + void ngOnDestroy() { |
| 150 | + _userSub.cancel(); |
| 151 | + } |
| 152 | + |
| 153 | + @override |
| 154 | + void ngOnInit() { |
| 155 | + _userSub = _authService.currentUser().listen((user) { |
| 156 | + _currentUser = user; |
| 157 | + _toggle(_checkCondition ? _currentUser != null : _currentUser == null); |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + void _toggle(bool show) { |
| 162 | + if (!show) { |
| 163 | + _signedOut(); |
| 164 | + } else { |
| 165 | + _signedIn(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + void _signedIn() { |
| 170 | + if (_lastCondition == true) return; |
| 171 | + _viewContainerRef |
| 172 | + .createEmbeddedView(_templateRef) |
| 173 | + .setLocal('currentUser', _currentUser); |
| 174 | + _lastCondition = true; |
| 175 | + } |
| 176 | + |
| 177 | + void _signedOut() { |
| 178 | + if (_lastCondition == false) return; |
| 179 | + _viewContainerRef.clear(); |
| 180 | + _lastCondition = false; |
| 181 | + } |
| 182 | +} |
0 commit comments