|
| 1 | +/** |
| 2 | + * Sample React Native App |
| 3 | + * https://github.com/facebook/react-native |
| 4 | + * |
| 5 | + * Generated with the TypeScript template |
| 6 | + * https://github.com/react-native-community/react-native-template-typescript |
| 7 | + * |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import React, {useState} from 'react'; |
| 12 | +import * as WebBrowser from '@toruslabs/react-native-web-browser'; |
| 13 | +import Web3Auth, { |
| 14 | + LOGIN_PROVIDER, |
| 15 | + OPENLOGIN_NETWORK, |
| 16 | + State, |
| 17 | +} from '@web3auth/react-native-sdk'; |
| 18 | + |
| 19 | +import {Button, StatusBar, StyleSheet, Text, View} from 'react-native'; |
| 20 | + |
| 21 | +import {Buffer} from 'buffer'; |
| 22 | + |
| 23 | +globalThis.Buffer = globalThis.Buffer || Buffer; |
| 24 | +const scheme = 'web3authexposample'; |
| 25 | +const resolvedRedirectUrl = `${scheme}://auth`; |
| 26 | + |
| 27 | +const App = () => { |
| 28 | + const [key, setKey] = useState(''); |
| 29 | + const [errorMsg, setErrorMsg] = useState(''); |
| 30 | + const [userInfo, setUserInfo] = useState<State | null>(null); |
| 31 | + const login = async () => { |
| 32 | + try { |
| 33 | + const web3auth = new Web3Auth(WebBrowser, { |
| 34 | + clientId: |
| 35 | + 'BA0mVyeHATikwuXVhXWCNjAxHthlw0w84mUhLuxlC4KZKjvmBsbdbmEWTizJ26YzrbKSWbOZbtGYdVDm0ESuYSg', |
| 36 | + network: OPENLOGIN_NETWORK.TESTNET, |
| 37 | + }); |
| 38 | + const state = await web3auth.login({ |
| 39 | + loginProvider: LOGIN_PROVIDER.GOOGLE, |
| 40 | + redirectUrl: resolvedRedirectUrl, |
| 41 | + }); |
| 42 | + setKey(state.privKey || 'no key'); |
| 43 | + setUserInfo(state); |
| 44 | + } catch (e) { |
| 45 | + console.error(e); |
| 46 | + setErrorMsg(String(e)); |
| 47 | + } |
| 48 | + }; |
| 49 | + return ( |
| 50 | + <View style={styles.container}> |
| 51 | + {key !== '' ? <Text>Key: {key}</Text> : null} |
| 52 | + {userInfo !== null ? ( |
| 53 | + <Text>UserInfo: {JSON.stringify(userInfo)}</Text> |
| 54 | + ) : null} |
| 55 | + {errorMsg !== '' ? <Text>Error: {errorMsg}</Text> : null} |
| 56 | + <Text>Linking URL: {resolvedRedirectUrl}</Text> |
| 57 | + <Button title="Login with Web3Auth" onPress={login} /> |
| 58 | + <StatusBar /> |
| 59 | + </View> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +const styles = StyleSheet.create({ |
| 64 | + sectionContainer: { |
| 65 | + marginTop: 32, |
| 66 | + paddingHorizontal: 24, |
| 67 | + }, |
| 68 | + sectionTitle: { |
| 69 | + fontSize: 24, |
| 70 | + fontWeight: '600', |
| 71 | + }, |
| 72 | + sectionDescription: { |
| 73 | + marginTop: 8, |
| 74 | + fontSize: 18, |
| 75 | + fontWeight: '400', |
| 76 | + }, |
| 77 | + highlight: { |
| 78 | + fontWeight: '700', |
| 79 | + }, |
| 80 | + container: { |
| 81 | + flex: 1, |
| 82 | + backgroundColor: '#fff', |
| 83 | + alignItems: 'center', |
| 84 | + justifyContent: 'center', |
| 85 | + }, |
| 86 | +}); |
| 87 | + |
| 88 | +export default App; |
0 commit comments