Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component } from '@angular/core';
- import { AngularFireAuth } from '@angular/fire/compat/auth';
- import { AngularFireDatabase } from '@angular/fire/compat/database';
- import { Observable, of, iif, switchMap, timer } from 'rxjs';
- const URL_IMAGE = 'IMAGE PATH;
- interface MyData {
- ble: {
- temp: number;
- pressure: number;
- altitude: number;
- }
- }
- @Component({
- selector: 'fb-demo-firebase',
- template: `
- <ng-template #showLogin>
- <h1 class="title-xl">Firebase Demo</h1>
- <button (click)="login()" class="btn btn-outline-secondary">
- <i class="fa fa-sign-in me-2"></i>Login with Google
- </button>
- </ng-template>
- <div *ngIf="auth.user | async as user; else showLogin">
- <h1 class="title-xl mt-5">
- <i class="fa fa-user"></i>
- Hello {{ user.email | json }}!
- </h1>
- <div class="row">
- <div *ngIf="data$ | async as data" class="col">
- <h1 class="title-xl mt-5">
- <i class="fa fa-thermometer-2"></i>
- {{data.ble.temp | number: '1.2-2'}} °
- </h1>
- <h1 class="title-xl mt-5">
- <i class="fa fa-arrows-v"></i>
- {{data.ble.altitude | number: '1.2-2'}} cm
- </h1>
- </div>
- <div class="col">
- <img [src]="url" alt="WebCam" width="100%" style="border-radius: 20px">
- </div>
- </div>
- <button (click)="logout()" class="btn btn-outline-secondary">
- <i class="fa fa-sign-out"></i>
- Logout
- </button>
- </div>
- `,
- })
- export class DemoFirebaseComponent {
- data$: Observable<MyData | null> = this.auth.user
- .pipe(
- switchMap(res => iif(
- () => !!res,
- this.db.object<MyData>(res.uid).valueChanges(),
- of(null)
- ))
- )
- url = URL_IMAGE;
- constructor(
- public auth: AngularFireAuth,
- private db: AngularFireDatabase,
- ) {
- timer(0, 1000)
- .subscribe(() => this.url = URL_IMAGE + Math.random())
- }
- login() {
- this.auth.signInWithEmailAndPassword('biondi@biondi.com', 'biondi')
- }
- logout() {
- this.auth.signOut();
- }
- }
Add Comment
Please, Sign In to add comment