Advertisement
Guest User

Untitled

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