Advertisement
Corjaantje

Untitled

May 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { AngularFireAuth } from 'angularfire2/auth';
  3. import { Router, CanActivate } from '@angular/router';
  4. import { firebase } from '@firebase/app';
  5. import { AngularFireDatabase } from 'angularfire2/database';
  6. import { User } from '../../models/user';
  7. import { Observable } from 'rxjs/Observable';
  8. import { CompetitionService } from '../competitionService/competition.service';
  9. import { UserService } from '../userService/user.service';
  10.  
  11. @Injectable()
  12. export class AuthService implements CanActivate {
  13.   defaultRole = 'player';
  14.  
  15.   constructor(
  16.     public afAuth: AngularFireAuth,
  17.     private router: Router,
  18.     private db: AngularFireDatabase,
  19.     private competitionService: CompetitionService,
  20.     private userService: UserService
  21.   ) {
  22.   }
  23.  
  24.   get authenticated(): boolean {
  25.     return this.userService.getLoggedInUserSource() !== null;
  26.   }
  27.  
  28.   get currentUser(): any {
  29.     return this.authenticated ? this.userService.getLoggedInUserSource() : null;
  30.   }
  31.   googleLogin() {
  32.     const promise = new Promise((resolve, reject) => {
  33.       const provider = new firebase.auth.GoogleAuthProvider();
  34.       provider.addScope('profile');
  35.       provider.addScope('email');
  36.       firebase
  37.         .auth()
  38.         .signInWithPopup(provider)
  39.         .then(result => {
  40.           if (result.user) {
  41.             const token = result.credential.accessToken;
  42.             this.doesUserExist(result.user, resolve);
  43.           } else {
  44.             reject();
  45.           }
  46.         });
  47.     });
  48.     return promise;
  49.   }
  50.  
  51.   doesUserExist(user, resolve) {
  52.     this.userService.getAllUsersObservable().subscribe(dbUsers => {
  53.       let alreadyExist = false;
  54.       for (const dbUser of dbUsers) {
  55.         const cast = dbUser as User;
  56.         if (cast.uid === user.uid) {
  57.           alreadyExist = true;
  58.           this.userService.setLoggedInUser(cast);
  59.         }
  60.       }
  61.       if (alreadyExist === false) {
  62.         const newUser = new User(user.uid, user.displayName, this.defaultRole);
  63.         this.db.list('/users/').push(newUser);
  64.         this.userService.setLoggedInUser(newUser);
  65.       }
  66.       resolve();
  67.     });
  68.   }
  69.  
  70.   signOut() {
  71.     const promise = new Promise((resolve, reject) => {
  72.       this.afAuth.auth.signOut();
  73.       this.userService.setLoggedInUser(null);
  74.       this.router.navigate(['/']);
  75.     });
  76.     return promise;
  77.   }
  78.  
  79.   canActivate(): boolean {
  80.     if (this.userService.getLoggedInUserSource() == null) {
  81.       this.router.navigate(['/']);
  82.       return false;
  83.     }
  84.     return true;
  85.   }
  86.  
  87.   isAdmin(): boolean {
  88.     if (this.userService.getLoggedInUserSource() != null && this.userService.getLoggedInUserSource().role === 'admin') {
  89.       return true;
  90.     } else {
  91.       return false;
  92.     }
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement