RxJS SwitchMap On each emission the previous inner observable is cancelled and the new observable is subscribed; switch to a new observable. Only one subscription at a time - seems ideal for login applications
💾 Setup
Install dependencies with npm i
Create google firebase project with Sign-in provider as Google and paste access keys from Project Settings into environment.ts
Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files
💻 Code Examples
user.service file
@Injectable({providedIn: 'root'})exportclassUserService{uid=this.afAuth.authState.pipe(map(authState=>{if(!authState){returnnull;}else{returnauthState.uid;}}));//test to see if already logged inisAdmin: Observable<boolean>=this.uid.pipe(switchMap(uid=>{if(!uid){returnobservableOf(false);}else{returnthis.db.object<boolean>('/admin/'+uid).valueChanges();}}));//test to see if user has specific permissionsconstructor(privateafAuth: AngularFireAuth,privatedb: AngularFireDatabase){}login(){this.afAuth.auth.signInWithPopup(newauth.GoogleAuthProvider());}logout(){this.afAuth.auth.signOut();}}
🆒 Features
user login using google id or password, can be viewed on Firebase Console.
User privileges can be revoked from the Firebase console.
📋 Status & To-Do List
Status: Working
To-Do: add theme colors and functionality. A bootstrap button would be better.