首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ReactJS登录ReactJS Facebook -facebook登录包

使用ReactJS登录ReactJS Facebook -facebook登录包
EN

Stack Overflow用户
提问于 2017-05-05 09:09:41
回答 2查看 3.1K关注 0票数 1

我试图在ReactJS中使用react facebook-登录包实现Facebook登录按钮。它非常简单,与提供的一段代码。

但是,在用户登录并且仍然有一个有效的令牌之后,按钮不会改变,并继续显示“登录脸书”。我可以在console.log中看到响应,包括名称图片idaccessTokenuserIDE 210

ButtonFacebookLogin.js:18

代码语言:javascript
复制
Object {name: "John Doe", picture: Object, id: "101560159xxxxxxx", 
accessToken: "EAAFdByROZC…5tDST3sxnhZASZBZ", userID: "101560159xxxxxxxx"…}

它应该自行更改,还是应该更改ReactJS脚本中的代码,以检查是否可以进行有效的登录?

代码语言:javascript
复制
import React from 'react';
import FacebookLogin from 'react-facebook-login';

class ButtonFacebookLogin extends React.Component {
  constructor(props) {
    super(props);

    this.responseFacebook = this.responseFacebook.bind(this);
    this.user = null;
  };

  responseFacebook(response) {
    console.log(response);
    this.user = response.name;
    console.log(this.user);
  };

  componentClicked(response) {
    console.log(response);
  };

  render() {
    return (
      <FacebookLogin
        appId="383760232021913"
        autoLoad={true}
        fields="name,email,picture"
        scope="public_profile,user_friends"
        onClick={this.componentClicked}
        callback={this.responseFacebook}
      />
      // just here above the button always looks the same
    )
  }

对不起,这个问题听起来可能很傻,因为我是ReactJS的新手:-)而且我在这个Facebook包上找不到很多文档。

EN

回答 2

Stack Overflow用户

发布于 2020-08-18 11:15:17

也许试试这个,从特拉维斯媒体那里得到密码,建议你查查他

代码语言:javascript
复制
export default class Facebook extends Component {
    state = {
      isLoggedIn: false,
      userID: "",
      name: "",
      email: "",
      picture: ""
    };
  
    
    responseFacebook = response => {
       console.log(response);
  
      this.setState({
        isLoggedIn: true,
        userID: response.userID,
        name: response.name,
        email: response.clientSecret,
        picture: response.picture.data.url
      });
    };
  
    componentClicked = () => console.log("clicked");

    render() {
      let fbContent;

      if (this.state.isLoggedIn) {
        fbContent = (
          <div
            style={{
              width: "400px",
              margin: "auto",
              background: "#grey",
              padding: "20px"
            }}
          >
            <img src={this.state.picture} alt={this.state.name} />
            <h2>Welcome {this.state.name}</h2>
            Email: {this.state.email}
          </div>
        );
      } else {
        fbContent = (
          <FacebookLogin
            appId="304489330816284"
            autoLoad={false}
            fields="name,email,picture"
            onClick={this.componentClicked}
            callback={this.responseFacebook}
           // scope="public_profile,user_friends,user_actions.Test"
          //  callback={this.responseFacebook}
          />
        );
      }
  
      return <div>{fbContent}</div>;
    }
  }
}
票数 4
EN

Stack Overflow用户

发布于 2017-05-05 15:50:09

是的,您必须检查它是否已经登录,并更改FacebookLogin组件的FacebookLogin字段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43800897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档