Advertisement
geeballer

users-service.ts

May 30th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Http } from '@angular/http';
  3. import 'rxjs/add/operator/map';
  4.  
  5. import * as firebase from 'firebase';
  6.  
  7. /*
  8.   Generated class for the UsersServiceProvider provider.
  9.  
  10.   See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  11.   for more info on providers and Angular 2 DI.
  12. */
  13. @Injectable()
  14. export class UsersServiceProvider {
  15.  
  16. private data: any;
  17. public fireAuth: any;
  18. public userProfile: any;
  19.  
  20.  
  21.   constructor(private http: Http) {
  22.       this.fireAuth = firebase.auth();
  23.       this.userProfile = firebase.database().ref('users');
  24.  
  25.     //console.log('Hello UsersServiceProvider Provider');
  26.   }
  27.  
  28.   loadUser(number){
  29.     if(this.data) {
  30.       return Promise.resolve(this.data);
  31.     }
  32.  
  33.     return new Promise(resolve => {
  34.  
  35.       this.http.get('https://randomuser.me/api/?results='+number)
  36.       .map(res => res.json())
  37.       .subscribe(data => {
  38.         this.data = data.results;
  39.         resolve(this.data);
  40.       })
  41.  
  42.     });
  43.  
  44.   }
  45.  
  46.   signUpUser(email: string, password: string){
  47.     return this.fireAuth.createUserWithEmailAndPassword(email, password).
  48.     then((newUser) => {
  49.         //SIGN IN THE USER
  50.       this.fireAuth.signInWithEmailAndPassword(email, password).then((authenticatedUser) => {
  51.         //SUCCESSFUL LOGIN, CREATE USER PROFILE
  52.         this.userProfile.child(authenticatedUser.uid).set({
  53.             email: email
  54.  
  55.         });
  56.        
  57.       });
  58.    
  59.    });
  60.  
  61. }
  62.  
  63. loginUser(email: string, password: string): any {
  64.   return this.fireAuth.signInWithEmailAndPassword(email, password);
  65.  
  66. }
  67.  
  68. logoutUser() {
  69.   return this.fireAuth.signOut();
  70. }
  71.  
  72. forgotPasswordUser(email: any){
  73.   return this.fireAuth.sendPasswordResetEmail(email);
  74.   }
  75. }
  76.  
  77. // googleSignInUser(){
  78.  
  79. //   var provider = new firebase.auth.GoogleAuthProvider();
  80. //   provider.addScope('https://www.googleapis.com/auth/plus.login');
  81.  
  82. //   var that = this;
  83.  
  84. //   return firebase.auth().signInWithPopup(provider).then(function(result) {
  85.  
  86. //     if (result.user) {
  87. //       console.log(result);
  88.  
  89. //       //The signed-in user info
  90. //       var user = result.user;
  91.  
  92. //         var res = result.user.displayName.split(" ");
  93.  
  94. //         that.userProfile.child(user.uid).set({
  95. //           email: user.email,
  96. //           photo: user.photoURL,
  97. //           username: user.displayName,
  98. //           name: {
  99. //             first: res[0],
  100. //             middle: res[1],
  101. //             last: res[2],
  102. //           }
  103. //         });
  104. //     }
  105. //   })
  106. // }
  107.  
  108. //   logOut():
  109.  
  110. //   firebase.Promise<void> {
  111. //     firebase.database().ref('/userProfile').child(firebase.auth().currentUser.uid).off();
  112. //           return firebase.auth().signOut();
  113. //   }
  114.  
  115. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement