Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 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. return this.fireAuth.createUserWithEmailAndPassword(email, password).then((newUser) => {
  52. //sign in the user
  53. this.fireAuth.signInWithEmailAndPassword(email, password).then((authenticatedUser) => {
  54. //successful login, create user profile
  55. this.userProfile.child(authenticatedUser.uid).set({
  56. email: email
  57. });
  58. });
  59. });
  60. }
  61.  
  62.  
  63. loginUser (email: string, password: string): any {
  64. return this.fireAuth.signInWithEmailAndPassword(email, password);
  65.  
  66.  
  67. }
  68.  
  69. logoutUser() {
  70.  
  71. return this.fireAuth.signOut();
  72.  
  73. //redirect after
  74.  
  75. }
  76.  
  77. forgotUserPassword(email: any) {
  78. return this.fireAuth.sendPasswordResetEmail(email);
  79. }
  80.  
  81. googleSignInUser(){
  82. var provider = new firebase.auth.GoogleAuthProvider();
  83. provider.addScope('https://www.googleapis.com/auth/plus.login');
  84.  
  85.  
  86.  
  87. var that = this;
  88.  
  89. return firebase.auth().signInWithPopup(provider).then(function(result) {
  90.  
  91. if (result.user) {
  92.  
  93. // The signed-in user info.
  94. var user = result.user;
  95.  
  96. var res = result.user.displayName.split(" ");
  97.  
  98.  
  99. that.userProfile.child(user.uid).set({
  100. email: user.email,
  101. photo: user.photoURL,
  102. username: user.displayName,
  103. name:{
  104. first: res[0],
  105. middle: res[1],
  106. last: res[2],
  107. },
  108. });
  109.  
  110.  
  111.  
  112. }
  113.  
  114. }).catch(function(error) {
  115. console.log(error);
  116. //alert("error "+error.message);
  117. });
  118. }
  119.  
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement