Code Monkey home page Code Monkey logo

express-torus-react-native's Introduction

express-torus-react-native

This library enables cross-platform (Android/iOS/Web/Expo) ethereum-login for React Native using Tor.us, a service which provides a simple and seamless way to onboard users from their social login into the world of Web3 and DeFi.

It works by navigating from your application to the interface hosted by your specified providerUri, i.e. where your instance of express-torus is being served. Upon successful auth with one of the many supported authentication providers, your app will returned to with the authentication result, which is accessed via a hook. This includes standard authentication data (such as username and profile photo) in addition to Ethereum wallet credentials that can be used in transactions.

This project was created as part of the Gitcoin KERNEL Genesis Block.

๐Ÿš€ Getting Started

Using npm:

npm install --save express-torus-react-native react-native-webview

Using yarn:

yarn add express-torus-react-native react-native-webview

Using Expo:

expo install express-torus-react-native react-native-webview

โœ๏ธ Usage

import React from "react";
import {Platform, SafeAreaView, TouchableOpacity, ActivityIndicator, StyleSheet, Text} from "react-native";

import Torus, {useTorus} from "express-torus-react-native";

const styles = StyleSheet.create({
  container: { flex: 1, backgroundColor: "lightgrey" },
  error: { color: "red" },
});

const ConnectedAccounts = ({ ...extraProps }) => {
  const { results, isLoggedIn, logout } = useTorus();
  return (
    <>
      {isLoggedIn && <Text children="Connected accounts:" />}
      {Object.entries(results).map(
        ([typeOfLogin, result], i) => (
          <Text 
            key={i}
            children={typeOfLogin}
          />
        )
      )}
      {isLoggedIn && (
        <TouchableOpacity
          onPress={logout}
        >
          <Text children="Logout" />
        </TouchableOpacity>
      )}
    </>
  );
};
const SimpleTorusLogin = ({...extraProps}) => {
  const {loading, results, login} = useTorus();
  if (loading) {
    return (
      <ActivityIndicator />
    );
  }
  return (
    <TouchableOpacity
      onPress={async () => {
        try {
          const result = await login();
          console.warn(result);
        } catch (e) {
          console.error(e);
        }
      }}
    >
      <Text children="Login" />
    </TouchableOpacity>
  );
};

export default function App() {
  return (
    <Torus>
      <SafeAreaView>
        <SimpleTorusLogin />
        <ConnectedAccounts />
      </SafeAreaView>
    </Torus>
  );
}

You can view the full list of supported authentication providers here.

โœŒ๏ธ License

MIT

express-torus-react-native's People

Contributors

cawfree avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.