You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Upgrading or Linking Firebase User Accounts
written by Jeff DelaneyThis snippet is useful for linking multiple accounts under the same Firebase UID with AngularFire2. We need to access the full firebase SDK, but it is still very easy to implement - just one line of code. Here we upgrade an anonymous user to Google, Facebook, or both.
Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { AngularFireAuth } from 'angularfire2/auth'; | |
import * as firebase from 'firebase/app'; | |
import { Observable } from 'rxjs/Observable'; | |
@Component({ | |
selector: 'app-user', | |
templateUrl: './user.component.html', | |
styleUrls: ['./user.component.scss'] | |
}) | |
export class UserComponent implements OnInit { | |
user: Observable<firebase.User>; | |
constructor(private afAuth: AngularFireAuth) { } | |
ngOnInit() { | |
this.user = this.afAuth.authState; | |
} | |
anonymousLogin() { | |
return this.afAuth.auth.signInAnonymously() | |
} | |
linkGoogle() { | |
const provider = new firebase.auth.GoogleAuthProvider() | |
firebase.auth().currentUser.linkWithPopup(provider) | |
} | |
linkFacebook() { | |
const provider = new firebase.auth.FacebookAuthProvider() | |
firebase.auth().currentUser.linkWithPopup(provider) | |
} | |
} |