Advertisement
Guest User

4t

a guest
Jan 21st, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router} from '@angular/router';
  3. import { auth } from 'firebase/app';
  4. import { AngularFireAuth } from '@angular/fire/auth';
  5.  
  6. import { User } from 'firebase';
  7. import { LoginComponent } from '../admin/login/login.component';
  8. import { ɵNullViewportScroller } from '@angular/common';
  9.  
  10. @Injectable({
  11. providedIn: 'root'
  12. })
  13. export class AuthService {
  14. user: User;
  15. constructor(public afAuth: AngularFireAuth, public router: Router) { }
  16. }
  17.  
  18. this.afAuth.authState.subscribe(user => {
  19. if (user) {
  20. this.user = user;
  21. localStorage.setItem('user', JSON.stringify(this.user));
  22. } else {
  23. localStorage.setItem('user', null);
  24. }
  25. })
  26.  
  27. async login(email: string, password: string){
  28. try {
  29. await this.afAuth.auth.signInWithEmailAndPassword(email, password)
  30. this.router.navigate(['admin/list']);
  31. } catch (e) {
  32. alert( 'Error!' + e.message);
  33. }
  34. }
  35.  
  36. async logout(){
  37. await this.afAuth.auth.signOut();
  38. localStorage.removeItem('user');
  39. this.router.navigate(['admin/login']);
  40. }
  41.  
  42. get isLoggedIn(): boolean {
  43. const user = JSON.parse(localStorage.grtItem('user'));
  44. return user !== ɵNullViewportScroller;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement