You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

49 lines
1.2 KiB

  1. import * as firebase from 'firebase';
  2. import { Router } from '@angular/router';
  3. import { Injectable } from '@angular/core';
  4. @Injectable()
  5. export class AuthService {
  6. token: string;
  7. constructor(private router: Router) {}
  8. signupUser(email: string, password: string) {
  9. firebase.auth().createUserWithEmailAndPassword(email, password).catch(
  10. error => console.log(error)
  11. );
  12. }
  13. signinUser(email: string, password: string) {
  14. firebase.auth().signInWithEmailAndPassword(email, password)
  15. .then(
  16. response => {
  17. this.router.navigate(['/']);
  18. firebase.auth().currentUser.getIdToken()
  19. .then(
  20. (token: string) => this.token = token
  21. );
  22. }
  23. )
  24. .catch(
  25. error => console.log(error)
  26. );
  27. }
  28. logout() {
  29. firebase.auth().signOut();
  30. this.token = null;
  31. }
  32. getToken() {
  33. firebase.auth().currentUser.getIdToken()
  34. .then(
  35. (token: string) => this.token = token
  36. );
  37. return this.token;
  38. }
  39. isAuthenticated() {
  40. return this.token != null;
  41. }
  42. }