Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用react在firebase中获取facebook uid?_Reactjs_Firebase_Firebase Realtime Database - Fatal编程技术网

Reactjs 如何使用react在firebase中获取facebook uid?

Reactjs 如何使用react在firebase中获取facebook uid?,reactjs,firebase,firebase-realtime-database,Reactjs,Firebase,Firebase Realtime Database,我在facebook上使用react和firebase auth。我正在使用以下代码使用facebook登录 const result = await auth().signInWithPopup(provider) 完整的代码是 import React, { Component } from 'react' import './StartPage.css' import fire from '../../fire.js' import { provider, auth } from '

我在facebook上使用react和firebase auth。我正在使用以下代码使用facebook登录

  const result = await auth().signInWithPopup(provider)
完整的代码是

import React, {
Component
} from 'react'
import './StartPage.css'
import fire from '../../fire.js'
import {
provider,
auth
} from '../../fire.js';
class StartPage extends Component {


constructor(props) {
    super(props)
    this.login = this.login.bind(this)

    this.loggedIn = this.loggedIn.bind(this)
}
async login() {
    provider.addScope('user_birthday');
    const result = await auth().signInWithPopup(provider)
    console.log(result)
      console.log(result.credential.accessToken)
    this.setState({
        user: result.user
    });
   console.log("Logged in")
    this.props.history.push('/vote')
}


loggedIn() {
    console.log('meh')
    // make check for login here
    this.props.history.push('/vote')
}

render() {

    return ( <
        div className = "StartPage" >
        <
        div className = "StartPage-up" >
        <
        div className = "StartPage-up-logo-container" >
        <
        img className = "StartPage-up-logo"
        src = "http://koyilandykoottam.in/images/home/logo.png" / >
        <
        /div> <
        div className = "StartPage-up-poy" > PERSON OF THE YEAR < /div> < /
        div > <
        div className = "StartPage-down" >
        <
        button className = "StartPage-facebook-login"
        onClick = {
            this.login
        } >
        Login with facebook <
        /button> < /
        div > <
        /div>
    )
}
}

export default StartPage
import-React{
组成部分
}从“反应”
导入“./StartPage.css”
从“../../fire.js”导入火
进口{
供应商,
认证
}来自“../fire.js”;
类StartPage扩展组件{
建造师(道具){
超级(道具)
this.login=this.login.bind(this)
this.loggedIn=this.loggedIn.bind(this)
}
异步登录(){
provider.addScope(“用户生日”);
const result=await auth().signInWithPopup(提供程序)
console.log(结果)
console.log(result.credential.accessToken)
这是我的国家({
用户:result.user
});
console.log(“登录”)
this.props.history.push(“/vote”)
}
loggedIn(){
console.log('meh')
//请在此处检查登录
this.props.history.push(“/vote”)
}
render(){
报税表(<
div className=“开始页面”>
<
div className=“开始页面向上”>
<
div className=“StartPage up徽标容器”>
<
img className=“开始页面向上徽标”
src=”http://koyilandykoottam.in/images/home/logo.png" / >
<
/div><
div className=“StartPage up poy”>年度人物/
div><
div className=“开始页面向下”>
<
button className=“StartPage facebook登录”
onClick={
请登录
} >
使用facebook登录<
/按钮><
/div>
)
}
}
导出默认起始页

Logging result.user仅显示登录用户的displayName和电子邮件id。有没有办法获取登录者的facebook uid?

您不会自动获取uid。你需要调用facebook的graph api来获取这些数据。一种方法是在登录后重定向时获取数据,因此

componentDidMount() {
    auth.getRedirectResult().then(result => {
      if (result.credential) {
        var token = result.credential.accessToken;
        axios
          .get(`https://graph.facebook.com/me?access_token=${token}&fields=id,name`
          )
          .then(result => console.log({ myData: result.data.data }))
          .catch(error => console.log(error));
      }
    });

  }
  • GraphAPI尝试不同的呼叫
  • 在用户呼叫中可以查询的所有内容的文档
试试这个:

firebase.auth().currentUser.providerData[0]。uid

更多信息,请访问:

您是否正确配置了fb应用程序并添加了所需的权限?是的,我正在获取用户的显示名和电子邮件id。我需要此人的uid来检查已有的数据库好的,显示您的代码,这样我可以帮助您更好地检查编辑