Advertisement
Guest User

Untitled

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