2016-10-18 2 views
-1

저는 Girebase에서 새로운입니다. 내 Ionic2 앱에서이 작업을 수행하려고합니다.특정 사용자에게 데이터를 삽입하고 firebase에서 자신의 데이터에 액세스하는 방법은 무엇입니까?

사용자가 Facebook에 로그인하면 데이터를 가져와 ID를 가져와 Firebase에 저장합니다. 그런 다음 특정 ID에 추가하고 싶습니다. 나는 그 모든 작업에 성공했지만 사용자로부터 로그 아웃 할 때 다시 로그인하고 모든 데이터가 삭제됩니다 (그의 노트 모두).

나는 사용자와 로그인 할 때마다 ID, 이름 및 사진을보기 위해 Firebase에 다시 전화하기 때문에 이전에 세부 사항을 제거하기 때문에 필자는 왜 그런 일이 일어 났는지 이해합니다. 소위 매번 홈페이지 : 이것은 내 페이스 북 로그인 코드의 내 중포 기지 구조 enter image description here

그래서

좋아

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 (직전 설명입니다 (그것을 잘 작동합니다) 그것은 again-- 어쩌면 데이터 프로파일을 설정하는 이동의

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 
    }); 
    } 

답변

0

. userID를 "메모"에 넣으면이 정보를 저장할 수 있습니다.

"사용자"는 사용자 이름, ID 및 포토 투어 만 저장하면됩니다.

관련 문제