Guest User

Untitled

a guest
Feb 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import { AngularFireAuth } from 'angularfire2/auth';
  2. import { Injectable } from '@angular/core';
  3. import * as firebase from 'firebase';
  4. import { Observable } from 'rxjs/Observable';
  5.  
  6. @Injectable()
  7. export class AuthService {
  8.  
  9. user$: Observable<firebase.User>;
  10.  
  11. constructor(private afAuth: AngularFireAuth) {
  12. this.user$ = afAuth.authState;
  13. }
  14.  
  15. getUser() {
  16. console.log(this.user$);
  17. }
  18.  
  19. loginWithEmailAndPassword(email, password) {
  20. this.afAuth.auth.signInWithEmailAndPassword(email, password);
  21. }
  22.  
  23. loginWithGoogle() {
  24. this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
  25. }
  26.  
  27. logOut() {
  28. this.afAuth.auth.signOut();
  29. }
  30.  
  31. }
  32.  
  33. import { Observable } from 'rxjs/Observable';
  34. import { AuthService } from './auth.service';
  35. import { Injectable } from '@angular/core';
  36. import { AngularFireDatabase } from 'angularfire2/database';
  37.  
  38. @Injectable()
  39. export class MyListService {
  40.  
  41. myFokos: Observable<any[]>;
  42.  
  43. constructor(auth: AuthService, database: AngularFireDatabase) {
  44. auth.user$.subscribe(user => {
  45. console.log('User is logged in. UID: ' + user.uid);
  46. this.myFokos = database.list('users/' + user.uid + '/glossaries').snapshotChanges();
  47. });
  48. }
  49.  
  50. }
  51.  
  52. import { AuthService } from './../../services/auth.service';
  53. import { MyListService } from './../../services/my-list.service';
  54. import { Component, OnInit } from '@angular/core';
  55.  
  56. @Component({
  57. selector: 'app-my-fokos',
  58. templateUrl: './my-fokos.component.html',
  59. styleUrls: ['./my-fokos.component.css']
  60. })
  61. export class MyFokosComponent implements OnInit {
  62.  
  63. constructor(public myListService: MyListService, public auth: AuthService) { }
  64.  
  65. ngOnInit() {
  66. this.auth.user$.subscribe(x => {
  67. console.log(x);
  68. });
  69. }
  70.  
  71. }
  72.  
  73. MyFokosComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'subscribe' of undefined
  74. at MyFokosComponent.ngOnInit (my-fokos.component.ts:15)
  75. at checkAndUpdateDirectiveInline (core.js:12364)
  76. at checkAndUpdateNodeInline (core.js:13888)
  77. at checkAndUpdateNode (core.js:13831)
  78. at debugCheckAndUpdateNode (core.js:14724)
  79. at debugCheckDirectivesFn (core.js:14665)
  80. at Object.eval [as updateDirectives] (MyFokosComponent_Host.ngfactory.js? [sm]:1)
  81. at Object.debugUpdateDirectives [as updateDirectives] (core.js:14650)
  82. at checkAndUpdateView (core.js:13797)
  83. at callViewAction (core.js:14148)
  84.  
  85. *ngFor="let foko of myListService.myFokos | async"
Add Comment
Please, Sign In to add comment