React Native Firebase Facebook登录有效,但成功登录后未通过身份验证

时间:2019-12-16 01:16:39

标签: firebase firebase-authentication facebook-authentication react-native-firebase

我已经通过react-native-firebase实现了facebook身份验证。当用户登录时,该用户会在信息中心中显示为通过Facebook进行身份验证。

我的问题是,我无法像使用电子邮件和密码身份验证那样使用firebase.auth()。currentUser来获取已登录的用户。

用户显示为空,并且我无法写入我的数据库(即使它说我已经成功登录)。

这是我的函数处理登录的样子:

    import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  Color specialColor = Color(0xffbfeb91);
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(flex: 1, child: ChessBoard()),
            Text(
              'Hello World',
              style: TextStyle(
                  backgroundColor: specialColor), // or 'bfeb91', or 'ffbfeb91'
            ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class ChessBoard extends StatefulWidget {
  @override
  _ChessBoardState createState() => _ChessBoardState();
}

class _ChessBoardState extends State<ChessBoard> {

  Color LIGHT_COLOR = Color(0xffeeeebb);
  Color LIGHT_COLOR_SELECTED = Color(0xffaaaaaa);
  Color DARK_COLOR = Color(0xff666666);
  Color DARK_COLOR_SELECTED = Color(0xff119911);

  //chesslib.Chess _chess;

  @override
  void initState() {
    super.initState();
    //_chess = new chesslib.Chess();
  }

  @override
  Widget build(BuildContext context) {

    Widget result;

    result = GridView.count(
      crossAxisCount: 8,
      children: List.generate(64, (index) {
        int row = index ~/ 8;
        int col = index % 8;
        int modulo = (row % 2 == 0) ? 0 : 1;

        Color color  = (index % 2 == modulo) ? Color(0xffeeeebb) : DARK_COLOR;

        return Container(
            color: color,
            child: Text ('display_str')
        );
      }),
    );

    return result;
  }
}

感谢您在此问题上能获得的所有帮助,欢呼!

1 个答案:

答案 0 :(得分:0)

这是我在我的本机应用程序中实现Facebook登录的我所做的事情,我在下面共享了代码,

import { AccessToken, LoginManager } from "react-native-fbsdk";
import firebase from "react-native-firebase";
import axios from "axios";
import { api } from "../config/api";
import { getToken } from './auth';
import {setData} from '../modules/localCache';
import {Platform} from 'react-native';

// Calling the following function will open the FB login dialogue:
export async function facebookLogin() {

  try {
    const result = await LoginManager.logInWithPermissions([
      "public_profile",
      "email",
    ]);

    if (result.isCancelled) {
      // handle this however suites the flow of your app
      throw new Error("User cancelled request");
    }



    // get the access token
    const data = await AccessToken.getCurrentAccessToken();

    if (!data) {
      // handle this however suites the flow of your app
      throw new Error("Something went wrong obtaining the users access token");
    }

    // create a new firebase credential with the token
    const credential = firebase.auth.FacebookAuthProvider.credential(
      data.accessToken,
    );

    //firebase token --> await firebase.getToken();


    // login with credential
    const firebaseUserCredential = await firebase
      .auth()
      .signInWithCredential(credential);

    // console.warn(JSON.stringify(firebaseUserCredential.user.toJSON()));

    let responseData = await firebaseUserCredential.user.toJSON();

在这里,我获得了带有电子邮件和Facebook ID等凭据的responseData。希望能帮助到你 。

如有疑问,请放心。会为您提供帮助。

相关问题