使用react native和redux实现FB登录

Peeyush Kumar

我想在基于React Native的应用程序中使用Redux框架来实现Facebook登录(目前我正在学习Redux)。我正在寻找有关如何构建我的Facebook登录代码以使用Redux的建议。更具体地说,我应该创建什么动作,reduce和存储?

以下是我在应用程序中使用的当前基于Facebook的登录代码(它不使用redux结构)。我删除了不相关的代码以使事情变得简单:

index.ios.js

class ProjectXApp extends React.Component {
  constructor(props) {
    // Set the use to NULL
    this.state = {
      user: null,
    };
  }

  handleLogin(user) {
    this.setState({
      // Update the user state once the login is complete
      user,
    });
  }

  renderScene(route, navigator) {
    const Component = route.component;

    return (
      <View style={styles.app}>
        <Component
          user={this.state.user}
          navigator={navigator}
          route={route}
        />
      </View>
    );
  }

  render() {
    return (
      <Navigator
        renderScene={this.renderScene.bind(this)}
        initialRoute={{
          // Render the Login page in the beginning
          component: Login,
          props: {
            onLogin: this.handleLogin.bind(this),
          },
        }}
      />
    );
  }
}

Login.js

// Import Facebook Login Util Component

class Login extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      // 'false' means responseToken is not required. 'true' means responseToken is required
      responseToken: false,
    };
  }

  // This method gets the fb access token, if the token is returned then 
  // I render the Main App component (switchToMain method). If the
  // access token is not returned then I render a login Button (Refer to render method)
  async getAccessToken() {
    let _this = this;
    await (FBSDKAccessToken.getCurrentAccessToken((token) => {
      if(!token) {
        _this.setState({responseToken: true})
        return;
      }

      _this.setState({responseToken: true});
      _this.props.route.props.onLogin({user: true});
      _this.switchToMain();
    }));
  }

  switchToMain() {
    this.props.navigator.push({
      component: Main, // Render the app
      props: {
        onLogOut: this.onLogOut.bind(this)
      }
    });
  }

  componentDidMount() {
    this.getAccessToken();
  }

  onLoginButtonPress() {
    // Shows transition between login and Main Screen
    this.setState({responseToken: false})
    FBSDKLoginManager.logInWithReadPermissions(['public_profile','email','user_friends'], (error, result) => {
      if (error) {
        alert('Error logging in');
      } else {
        if (result.isCancelled) {
          alert('Login cancelled');
        } else {
          this.setState({result});
          this.getAccessToken();
        }
      }
    });
  }

  onLogOut() {
    this.setState({responseToken: true});
  }

  render() {
    // This component renders when I am calling getAccessToken method
    if(!this.state.responseToken) {
      return (
        <Text></Text>
      );
    }

    // This renders when access token is not available after calling getAccessToken
    return (
      <View style={styles.container}>
        <TouchableHighlight
          onPress={this.onLoginButtonPress.bind(this)}
          >
          <View>
            // Login Button
          </View>
        </TouchableHighlight>
      </View>
    );
  }
}

// Removed the styling code

注销.js

import { FBSDKLoginManager } from 'react-native-fbsdklogin';

class Logout extends React.Component {
  onLogOut() {
    FBSDKLoginManager.logOut();
    this.props.onLogOut();
    this.props.navigator.popToTop();
  }

  render() {
    return (
      <View>
        <TouchableHighlight
          onPress={this.onLogOut.bind(this)}
          >
          <View
            // Styles to create Logout button
          </View>
        </TouchableHighlight>
      </View>
    );
  }
});

// Removed the styling code
德米特里·谢韦多夫(Dmitry Shvedov)

您是否看过这个库:https : //github.com/lynndylanhurley/redux-auth

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 React Native + AWS Cognito 登录 FB

来自分类Dev

在使用react-redux实现react-native时出错

来自分类Dev

您将如何使用Redux和Immutablejs实现React Native的NavigationExperimental?

来自分类Dev

使用React,redux和react-redux-router登录验证:正确的方法是?

来自分类Dev

使用 react native 和 redux 进行异步调用,thunk

来自分类Dev

登录后如何使用react-redux和react-aad-msal触发操作?

来自分类Dev

如何使用 React、Redux 和 Redux-Saga 实现乐观状态更新/推送

来自分类Dev

React-native 处理登录和注册

来自分类Dev

在 React 和 React Native 中使用模型

来自分类Dev

在React Native中使用Azure登录

来自分类Dev

使用 facebook React Native Feathersjs 登录

来自分类Dev

React Native-NavigatorIOS和TabBarIOS实现

来自分类Dev

React Native-NavigatorIOS和TabBarIOS实现

来自分类Dev

如何使用React Native实现Firebase?

来自分类Dev

登录后使用react-router-v5和redux-toolkit重定向页面

来自分类Dev

React:使用React-router实现Redux麻烦

来自分类Dev

React native、redux 和 axios:UnhandledPromiseRejectionWarning

来自分类Dev

React-native 和 redux 操作

来自分类Dev

在 Redux 的 Action 中,使用回调调用函数(使用 async/await 和 React-Native-Contacts)

来自分类Dev

在React / React Native(Redux)中使用Spread运算符

来自分类Dev

React Native FBSDK 登录 - 使用应用程序登录失败

来自分类Dev

如何使用 Facebook Login 和 ReactJS 实现登录和注销?

来自分类Dev

使用React和Redux的发布方法

来自分类Dev

使用React-Native和Redux在Actions中访问导航器

来自分类Dev

Redux和Context API在同一React Native项目中一起使用

来自分类Dev

使用 Redux 和 React Native 将值传递给动作创建者

来自分类Dev

如何在 React Native 中正确使用 redux saga 和后台事件?

来自分类Dev

使用React Redux登录后重定向用户?

来自分类Dev

使用React Redux登录后重定向用户?

Related 相关文章

  1. 1

    使用 React Native + AWS Cognito 登录 FB

  2. 2

    在使用react-redux实现react-native时出错

  3. 3

    您将如何使用Redux和Immutablejs实现React Native的NavigationExperimental?

  4. 4

    使用React,redux和react-redux-router登录验证:正确的方法是?

  5. 5

    使用 react native 和 redux 进行异步调用,thunk

  6. 6

    登录后如何使用react-redux和react-aad-msal触发操作?

  7. 7

    如何使用 React、Redux 和 Redux-Saga 实现乐观状态更新/推送

  8. 8

    React-native 处理登录和注册

  9. 9

    在 React 和 React Native 中使用模型

  10. 10

    在React Native中使用Azure登录

  11. 11

    使用 facebook React Native Feathersjs 登录

  12. 12

    React Native-NavigatorIOS和TabBarIOS实现

  13. 13

    React Native-NavigatorIOS和TabBarIOS实现

  14. 14

    如何使用React Native实现Firebase?

  15. 15

    登录后使用react-router-v5和redux-toolkit重定向页面

  16. 16

    React:使用React-router实现Redux麻烦

  17. 17

    React native、redux 和 axios:UnhandledPromiseRejectionWarning

  18. 18

    React-native 和 redux 操作

  19. 19

    在 Redux 的 Action 中,使用回调调用函数(使用 async/await 和 React-Native-Contacts)

  20. 20

    在React / React Native(Redux)中使用Spread运算符

  21. 21

    React Native FBSDK 登录 - 使用应用程序登录失败

  22. 22

    如何使用 Facebook Login 和 ReactJS 实现登录和注销?

  23. 23

    使用React和Redux的发布方法

  24. 24

    使用React-Native和Redux在Actions中访问导航器

  25. 25

    Redux和Context API在同一React Native项目中一起使用

  26. 26

    使用 Redux 和 React Native 将值传递给动作创建者

  27. 27

    如何在 React Native 中正确使用 redux saga 和后台事件?

  28. 28

    使用React Redux登录后重定向用户?

  29. 29

    使用React Redux登录后重定向用户?

热门标签

归档