如何将数据插入特定用户并在firebase中访问他们自己的数据?

时间:2016-10-18 15:28:16

标签: firebase firebase-realtime-database firebase-authentication

我是Girebase的新手。在我的Ionic2应用程序中,我正在尝试这样做:

当用户使用Facebook登录时,它会获取他的数据并获取ID并将其放入Firebase中。他补充说,然后我想添加特定ID。 我成功完成了所有这些操作,但是当我从用户注销时,我再次登录并删除所有数据(所有注释)。

我理解为什么会发生这种情况,因为每次我与用户登录时,都会再次致电Firebase查看ID,姓名和照片,然后才删除详细信息。

好的,这是我的Firebase结构 enter image description here

这是我的Facebook登录代码(工作正常)

   facebookLogin(){

    Facebook.login(['email']).then( (response) => {
      let facebookCredential = firebase.auth.FacebookAuthProvider
          .credential(response.authResponse.accessToken);
      var that = this;
      firebase.auth().signInWithCredential(facebookCredential)
          .then((success) => {
            console.log("Firebase success: " + JSON.stringify(success));  
            that.nav.setRoot(HomePage);
          })
          .catch((error) => {
            console.log("Firebase failure: " + JSON.stringify(error));
          });

    }).catch((error) => { console.log(error) });
  }

这是home.ts (之前解释一下:每次主页调用时都会再次设置数据配置文件 - 也许是因为问题所在)

import { Component } from '@angular/core';

import { NavController,NavParams,LoadingController,AlertController,ViewController  } from 'ionic-angular';
import { Facebook } from 'ionic-native';

//import pages
import {LoginPage} from "../../pages/login/login";
import {User} from '../../models/user'

//import provider
import { ProfileData } from '../../providers/profile-data';
import { NotesData } from '../../providers/notes-data';

import firebase from 'firebase'
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {
  //facebook user
  userProfile: any = null;
  uid: any = null;
  fireUid:any=null;
  name:string=null;
  photo: any = null;
  user:User=null;

  //notes list

  notes:any=null;
  data:any;



  constructor(public navCtrl: NavController, public navParams: NavParams,public profileData:ProfileData,private viewCtrl: ViewController,public notesData:NotesData,private loadingCtrl: LoadingController,private alertCtrl: AlertController) {
    this.data={};
    this.data.title="";
    this.data.desc="";
  }
  ionViewDidLoad() {
    this.fireUid=firebase.auth().currentUser.uid;
    this.getDetailsFacebook();
    this.getNotesList();
  }

//facebook functions
  getDetailsFacebook() {
    var that=this;
    Facebook.getLoginStatus().then((response)=> {
      if (response.status == 'connected') {
        Facebook.api('/' + response.authResponse.userID + '?fields=id,name,gender', []).then((response)=> {
          //alert(JSON.stringify(response));
         that.uid = response.id;
          that.name=response.name;
         that.photo = "http://graph.facebook.com/"+that.uid+"/picture?type=large";
          that.user=new User(that.uid,that.fireUid,that.name, that.photo);
          that.profileData.setProfileData(that.user); // to create class for that

          //that.profileData.setProfile(that.uid,that.name,that.photo);
          //console.log("id:"+this.uid+this.name+this.photo);
        }, (error)=> {
          alert(error);
        })
      }
      else {
        alert('Not Logged in');
      }
    })


  }
  getPhoto() {
    var that=this;
    Facebook.getLoginStatus().then((response)=> {
      if (response.status == 'connected') {
        Facebook.api('/me', []).then((response)=> {
          alert(JSON.stringify(response));
          that.photo = "http://graph.facebook.com/"+response.that.uid+"/picture";
        }, (error)=> {
          alert(error);
        })
      }
      else {
        alert('Not Logged in');
      }
    })


  }
  logOutFacebook(){
    Facebook.logout().then((response)=>
    {
      this.navCtrl.push(LoginPage);
      alert(JSON.stringify(response));
    },(error)=>{
      alert(error);
    })
  }

  //notes functions
  getNotesList(){
    console.log("get event");
    var that=this;
    this.notesData.getNotesLIst().on('value', snapshot => {
      let notesList= [];
      snapshot.forEach( snap => {
        console.log("id note"+snap.val().id);
        notesList.push({
          id: snap.val().id,
          title: snap.val().title,
          desc: snap.val().desc,
        });
      });
      that.notes = notesList;
    });
  }
  addNotes() {
    //add preloader

    console.log(this.data.title,this.data.desc);
    this.notesData.createNote(this.data.title, this.data.desc);

    }

  delete(id:number){
  }
  update(id:number){}
}

这是我的个人资料数据提供者

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import firebase from 'firebase'

//import pages
import {User} from '../models/user'
/*
  Generated class for the ProfileData provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/

@Injectable()
export class ProfileData {

  public userProfile: any;
  public currentUser: any;

  constructor() {
    this.currentUser = firebase.auth().currentUser; // We'll use this to create a database reference to the userProfile node.
    this.userProfile = firebase.database().ref('/users'); // We'll use this to create an auth reference to the current user.
  }
  setProfileData(userObj:User){
    this.userProfile.child(userObj.fireUid).set({
      firstname:userObj.full_name,
      photoURl:userObj.photo
    });
  }

1 个答案:

答案 0 :(得分:0)

您必须在“用户”之外创建“备注”。您可以将userID放在“notes”中,这样​​您就可以保存此信息。

因为“用户”只保存用户名,身份证和照片。