|
| 1 | +# Upgrading to AngularFire2 4.0 |
| 2 | + |
| 3 | +AngularFire2 4.0 is a refactor of the AngularFire2 package which implements |
| 4 | +@NgModule, simplifies authentication, and better supports Angular 4. |
| 5 | + |
| 6 | +The proposal was introduced by David East in [Release Candidate API proposal |
| 7 | +#854](https://github.com/angular/angularfire2/issues/854). |
| 8 | + |
| 9 | +### Removing `AngularFire` for Modularity |
| 10 | + |
| 11 | +AngularFire2 does not take advantage of the Firebase SDK's modularity. Users who only need authentication receive database code and vice versa. The `AngularFire` service is a central part of this problem. The class includes each feature whether you are using it or not. Even worse, this cannot be tree-shaken. As the library grows to include more features, this will only become more of a problem. |
| 12 | + |
| 13 | +The way to fix this is to remove the `AngularFire` service and break up the library into smaller @NgModules. |
| 14 | + |
| 15 | +* `AngularFireModule` |
| 16 | +* `AngularFireDatabaseModule` |
| 17 | +* `AngularFireAuthModule` |
| 18 | + |
| 19 | +### Simplified Authentication API |
| 20 | + |
| 21 | +The goal of AngularFire is not to wrap the Firebase SDK. **The goal is to provide Angular-specific functionality.** |
| 22 | + |
| 23 | +To this effect, we've cut the auth module down to an obverser for state changes. |
| 24 | + |
| 25 | +Most of the custom authentication methods were simply wrapper calls around the official SDK. Rather than creating more bytes for no value, the Firebase Auth instance is available as a property named `auth` where the same functionality exists. |
| 26 | + |
| 27 | +### Removing pre-configured login |
| 28 | + |
| 29 | +While convenient, the pre-configure login feature added unneeded complexity into our codebase. You can now trigger login as expected with the Firebase SDK. |
| 30 | + |
| 31 | +### FirebaseListFactory and FirebaseObjectFactory API Changes |
| 32 | + |
| 33 | +This change only affects a few users who directly use `FirebaseListFactory`. Most people inject `AngularFireDatabase` directly or use the `AngularFire` service. |
| 34 | + |
| 35 | +If you directly use `FirebaseListFactory` you will no longer be able to pass in a string. **The `AngularFireDatabase` module will still take paths for .list() and .object().** With the `FirebaseApp` now easily injectable, you can create a reference while still adhering to DI. |
| 36 | + |
| 37 | +This is a minor change, but again it prioritizes simplicity. It keeps the library from writing check logic at multiple abstractions. This again reduces complexity and maintenance. |
| 38 | + |
| 39 | +## Putting this all together |
| 40 | + |
| 41 | +Here's an example of what AngularFire2 4.0 looks like: |
| 42 | + |
| 43 | +``` |
| 44 | +import { NgModule, Component } from '@angular/core'; |
| 45 | +import { Observable } from 'rxjs/Observable'; |
| 46 | +import { AngularFireModule } from 'angularfire2'; |
| 47 | +import { AngularFireDatabaseModule, AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; |
| 48 | +import { AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth'; |
| 49 | +import { GoogleAuthProvider, User as FirebaseUser } from 'firebase/auth'; |
| 50 | +import { environment } from '../environments/environment'; |
| 51 | +
|
| 52 | +@NgModule({ |
| 53 | + declarations: [ App ], |
| 54 | + exports: [ App ], |
| 55 | + imports: [ |
| 56 | + AngularFireModule.initializeApp(environment.firebase, 'my-app') |
| 57 | + AngularFireDatabaseModule, |
| 58 | + AngularFireAuthModule, |
| 59 | + ], |
| 60 | + bootstrap[ App ] |
| 61 | +}) |
| 62 | +export class MyModule { } |
| 63 | +
|
| 64 | +@Component({ |
| 65 | + selector: 'my-app', |
| 66 | + template: ` |
| 67 | + <div> {{ (items | async)? | json }} </div> |
| 68 | + <div> {{ (user | async)? | json }} </div> |
| 69 | + <button (click)="login()">Login</button> |
| 70 | + <button (click)="logout()">Logout</button> |
| 71 | + ` |
| 72 | +}) |
| 73 | +export class App { |
| 74 | + user: Observable<FirebaseUser>; |
| 75 | + items: FirebaseListObservable<any[]>; |
| 76 | + constructor(afAuth: AngularFireAuth, db: AngularFireDatabase) { |
| 77 | + this.user = afAuth.authState; |
| 78 | + this.items = db.list('items'); |
| 79 | + } |
| 80 | + login() { |
| 81 | + this.afAuth.auth.signInWithPopup(new GoogleAuthProvider()); |
| 82 | + } |
| 83 | + logout() { |
| 84 | + this.afAuth.auth.signOut(); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
0 commit comments