Advertisement
Guest User

Untitled

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