将ionic 2 nativ facebook与firebase联系起来的最佳方式

时间:2016-08-19 10:09:11

标签: ionic-framework firebase ionic2 firebase-authentication

目前正在我的离子2应用程序中实现signIn。

我想使用离子2原生facebook并以某种方式将数据保存到我的firebase应用程序。

有没有办法存档?

一种方法是使用Facebook电子邮件地址和一些密码哈希创建一个新的firebase auth用户,但也许有更好的解决方案。

这是我到目前为止所得到的(我知道,并不多):)

import {NavController, Loading, Platform, Storage, LocalStorage} from "ionic-angular";
import {OnInit, Inject, Component} from "@angular/core";

import {ForgotPasswordPage} from "../forgot-password/forgot-password";
import {SignUpPage} from "../sign-up/sign-up";
import {HomePage} from "../../home/home";
import * as firebase from 'firebase';
import {Facebook} from 'ionic-native';

/*
 Generated class for the LoginPage page.

 See http://ionicframework.com/docs/v2/components/#navigation for more info on
 Ionic pages and navigation.
 */
@Component({
    templateUrl: 'build/pages/auth/login/login.html',
})
export class LoginPage {
    private local: any;

    constructor(private navCtrl: NavController, private platform:Platform) {
        this.local = new Storage(LocalStorage);
    }



openForgotPasswordPage():void {
     this.navCtrl.push(ForgotPasswordPage);
}

openSignUpPage():void {
    this.navCtrl.push(SignUpPage);
}

login() {
    firebase.auth().signInWithEmailAndPassword("test@test.com", "correcthorsebatterystaple").then(function (result) {
        console.log("AUTH OK "+ result);
    }, function (error) {
        console.log("dawdaw");
    });
}

facebookLogin() {
    Facebook.login(['public_profile', 'user_birthday']).then(() => {
        this.local.set('logged', true);
        this.navCtrl.setRoot(HomePage);
    }, (...args) => {
        console.log(args);
    })
} }

2 个答案:

答案 0 :(得分:1)

facebookLogin() {
  Facebook.login(['public_profile', 'user_birthday']).then((result) => {
    var creds = firebase.auth.FacebookAuthProvider.credential(result.access_token);
    return firebase.auth().signInWithCredential(creds);
  })
  .then((_user) => {
    console.log("_user:", _user);
  })
  .catch((_error) => {
    console.error("Error:", _error);
  });
}

在此处查看更多信息 - https://firebase.google.com/docs/auth/web/facebook-login#advanced-handle-the-sign-in-flow-manually

答案 1 :(得分:0)

我没有尝试过这个,所以可能不会百分之百地工作,但试试这个Gist我发现:https://gist.github.com/katowulf/de9ef6b04552091864fb807092764224