Guest User

Untitled

a guest
Feb 2nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { Observable } from 'rxjs/Observable';
  4. import * as firebase from 'firebase/app';
  5. import { AngularFireAuth } from 'angularfire2/auth';
  6.  
  7. @Injectable()
  8.  
  9. export class AuthService {
  10.  
  11. private user: Observable<firebase.User>;
  12.  
  13. isAuthenticated: boolean = false;
  14.  
  15. constructor(private firebaseAuth: AngularFireAuth, private router: Router) {
  16. this.user = firebaseAuth.authState;
  17. }
  18.  
  19. signIn(email: string, password: string) {
  20. this.firebaseAuth
  21. .auth
  22. .signInWithEmailAndPassword(email, password)
  23. .then(value => {
  24. console.log('Signed In');
  25. this.isAuthenticated = true;
  26. this.router.navigateByUrl('/dashboard');
  27. })
  28. .catch(err => {
  29. console.log('Sign-In Error: ', err.message);
  30. });
  31. }
  32.  
  33. signOut() {
  34. this.firebaseAuth
  35. .auth
  36. .signOut();
  37. this.isAuthenticated = false;
  38. this.router.navigateByUrl('/login');
  39. console.log('Signed Out');
  40. }
  41.  
  42. register(email: string, password: string) {
  43. this.firebaseAuth
  44. .auth
  45. .createUserWithEmailAndPassword(email, password)
  46. .then(value => {
  47. console.log('Registration Successful', value);
  48. })
  49. .catch(err => {
  50. console.log('Registration Error: ', err.message);
  51. });
  52. }
  53.  
  54. }
  55.  
  56. import { Injectable } from '@angular/core';
  57. import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
  58. import { Observable } from 'rxjs/Observable';
  59.  
  60. import { AuthService } from './services/auth.service';
  61.  
  62. @Injectable()
  63.  
  64. export class CanActivateRouteGuard implements CanActivate {
  65.  
  66. constructor(private auth: AuthService) { }
  67.  
  68. canActivate (
  69. route: ActivatedRouteSnapshot,
  70. state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  71. return this.auth.isAuthenticated;
  72. }
  73.  
  74. }
  75.  
  76. import { Component } from '@angular/core';
  77. import { Router } from '@angular/router';
  78. import { Observable } from 'rxjs/Observable';
  79.  
  80. import { AngularFirestore } from 'angularfire2/firestore';
  81.  
  82. import { UsersService } from './services/users.service';
  83. import { AuthService } from './services/auth.service';
  84.  
  85. @Component({
  86. selector: 'app-root',
  87. templateUrl: './app.component.html',
  88. styleUrls: ['./app.component.scss']
  89. })
  90.  
  91. export class AppComponent {
  92.  
  93. isAuthenticated: boolean;
  94. titles: any[];
  95.  
  96. constructor(public db: AngularFirestore, private usersService: UsersService, public authService: AuthService, private router: Router) {
  97. this.isAuthenticated = this.authService.isAuthenticated;
  98. }
  99.  
  100. <button mat-button (click)="signOut()" [hidden]="isAuthenticated">Sign Out</button>
Add Comment
Please, Sign In to add comment