-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e303c3
commit 55647c9
Showing
37 changed files
with
10,779 additions
and
57,708 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,10 @@ import '@ethersproject/shims'; | |
import auth from '@react-native-firebase/auth'; | ||
// IMP END - Auth Provider Login | ||
import EncryptedStorage from 'react-native-encrypted-storage'; | ||
import {decode as atob} from 'base-64'; | ||
import {IProvider} from '@web3auth/base'; | ||
import {CHAIN_NAMESPACES, IProvider, WEB3AUTH_NETWORK} from '@web3auth/base'; | ||
|
||
// IMP START - Quick Start | ||
import Web3Auth from '@web3auth/single-factor-auth-react-native'; | ||
import {Web3Auth, SDK_MODE, decodeToken} from '@web3auth/single-factor-auth'; | ||
import {EthereumPrivateKeyProvider} from '@web3auth/ethereum-provider'; | ||
// IMP END - Quick Start | ||
import {ethers} from 'ethers'; | ||
|
@@ -47,22 +46,28 @@ async function signInWithEmailPassword() { | |
|
||
// IMP START - SDK Initialization | ||
const chainConfig = { | ||
chainId: '0x1', // Please use 0x1 for Mainnet | ||
rpcTarget: 'https://rpc.ankr.com/eth', | ||
chainId: '0x1', | ||
displayName: 'Ethereum Mainnet', | ||
blockExplorer: 'https://etherscan.io/', | ||
ticker: 'ETH', | ||
chainNamespace: CHAIN_NAMESPACES.EIP155, | ||
tickerName: 'Ethereum', | ||
ticker: 'ETH', | ||
decimals: 18, | ||
rpcTarget: 'https://rpc.ankr.com/eth', | ||
blockExplorerUrl: 'https://etherscan.io', | ||
logo: 'https://cryptologos.cc/logos/ethereum-eth-logo.png', | ||
}; | ||
|
||
const web3auth = new Web3Auth(EncryptedStorage, { | ||
clientId, // Get your Client ID from Web3Auth Dashboard | ||
web3AuthNetwork: 'sapphire_mainnet', | ||
}); | ||
|
||
const privateKeyProvider = new EthereumPrivateKeyProvider({ | ||
config: {chainConfig}, | ||
}); | ||
|
||
const web3auth = new Web3Auth({ | ||
clientId, // Get your Client ID from Web3Auth Dashboard | ||
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET, | ||
privateKeyProvider, | ||
storage: EncryptedStorage, | ||
mode: SDK_MODE.REACT_NATIVE, | ||
}); | ||
// IMP END - SDK Initialization | ||
|
||
export default function App() { | ||
|
@@ -76,7 +81,7 @@ export default function App() { | |
const init = async () => { | ||
try { | ||
// IMP START - SDK Initialization | ||
await web3auth.init(privateKeyProvider); | ||
await web3auth.init(); | ||
setProvider(web3auth.provider); | ||
// IMP END - SDK Initialization | ||
|
||
|
@@ -91,20 +96,8 @@ export default function App() { | |
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const parseToken = (token: any) => { | ||
try { | ||
const base64Url = token.split('.')[1]; | ||
const base64 = base64Url.replace('-', '+').replace('_', '/'); | ||
return JSON.parse(atob(base64 || '')); | ||
} catch (err) { | ||
uiConsole(err); | ||
return null; | ||
} | ||
}; | ||
|
||
const login = async () => { | ||
try { | ||
setConsoleUI('Logging in'); | ||
setLoading(true); | ||
// IMP START - Auth Provider Login | ||
const loginRes = await signInWithEmailPassword(); | ||
|
@@ -114,11 +107,12 @@ export default function App() { | |
const idToken = await loginRes!.user.getIdToken(true); | ||
// IMP END - Login | ||
uiConsole('idToken', idToken); | ||
const parsedToken = parseToken(idToken); | ||
const parsedToken = decodeToken(idToken); | ||
uiConsole('parsed token', parsedToken); | ||
setUserInfo(parsedToken); | ||
|
||
// IMP START - Login | ||
const verifierId = parsedToken.sub; | ||
const verifierId = parsedToken.payload.sub; | ||
await web3auth!.connect({ | ||
verifier, // e.g. `web3auth-sfa-verifier` replace with your verifier name, and it has to be on the same network passed in init(). | ||
verifierId, // e.g. `Yux1873xnibdui` or `[email protected]` replace with your verifier id(sub or email)'s value. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 63 additions & 19 deletions
82
single-factor-auth-react-native/sfa-rn-bare-quick-start/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,79 @@ | ||
# Web3Auth SFA React Native Bare QuickStart | ||
This is a new [**React Native**](https://reactnative.dev) project, bootstrapped using [`@react-native-community/cli`](https://github.com/react-native-community/cli). | ||
|
||
[![Web3Auth](https://img.shields.io/badge/Web3Auth-SDK-blue)](https://web3auth.io/docs/sdk/core-kit/sfa-react-native) | ||
[![Web3Auth](https://img.shields.io/badge/Web3Auth-Community-cyan)](https://web3auth.io/community) | ||
# Getting Started | ||
|
||
[Join our Community Portal](https://web3auth.io/community) to get support and stay up to date with the latest news and updates. | ||
>**Note**: Make sure you have completed the [React Native - Environment Setup](https://reactnative.dev/docs/environment-setup) instructions till "Creating a new application" step, before proceeding. | ||
This example demonstrates how to use Web3Auth in React Native. | ||
## Step 1: Start the Metro Server | ||
|
||
## How to Use | ||
First, you will need to start **Metro**, the JavaScript _bundler_ that ships _with_ React Native. | ||
|
||
### Download Manually | ||
To start Metro, run the following command from the _root_ of your React Native project: | ||
|
||
```bash | ||
npx degit Web3Auth/web3auth-core-kit-examples/single-factor-auth-react-native/sfa-rn-bare-quick-start w3a-sfa-rn-bare-quick-start | ||
# using npm | ||
npm start | ||
|
||
# OR using Yarn | ||
yarn start | ||
``` | ||
|
||
Install & Run: | ||
## Step 2: Start your Application | ||
|
||
Let Metro Bundler run in its _own_ terminal. Open a _new_ terminal from the _root_ of your React Native project. Run the following command to start your _Android_ or _iOS_ app: | ||
|
||
### For Android | ||
|
||
```bash | ||
cd w3a-sfa-rn-bare-quick-start | ||
npm install | ||
# using npm | ||
npm run android | ||
# or | ||
|
||
# OR using Yarn | ||
yarn android | ||
``` | ||
|
||
### For iOS | ||
|
||
```bash | ||
# using npm | ||
npm run ios | ||
|
||
# OR using Yarn | ||
yarn ios | ||
``` | ||
|
||
## Important Links | ||
If everything is set up _correctly_, you should see your new app running in your _Android Emulator_ or _iOS Simulator_ shortly provided you have set up your emulator/simulator correctly. | ||
|
||
This is one way to run your app — you can also run it directly from within Android Studio and Xcode respectively. | ||
|
||
## Step 3: Modifying your App | ||
|
||
Now that you have successfully run the app, let's modify it. | ||
|
||
1. Open `App.tsx` in your text editor of choice and edit some lines. | ||
2. For **Android**: Press the <kbd>R</kbd> key twice or select **"Reload"** from the **Developer Menu** (<kbd>Ctrl</kbd> + <kbd>M</kbd> (on Window and Linux) or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> (on macOS)) to see your changes! | ||
|
||
For **iOS**: Hit <kbd>Cmd ⌘</kbd> + <kbd>R</kbd> in your iOS Simulator to reload the app and see your changes! | ||
|
||
## Congratulations! :tada: | ||
|
||
You've successfully run and modified your React Native App. :partying_face: | ||
|
||
### Now what? | ||
|
||
- If you want to add this new React Native code to an existing application, check out the [Integration guide](https://reactnative.dev/docs/integration-with-existing-apps). | ||
- If you're curious to learn more about React Native, check out the [Introduction to React Native](https://reactnative.dev/docs/getting-started). | ||
|
||
# Troubleshooting | ||
|
||
If you can't get this to work, see the [Troubleshooting](https://reactnative.dev/docs/troubleshooting) page. | ||
|
||
# Learn More | ||
|
||
To learn more about React Native, take a look at the following resources: | ||
|
||
- [Website](https://web3auth.io) | ||
- [Docs](https://web3auth.io/docs) | ||
- [Guides](https://web3auth.io/docs/guides) | ||
- [SDK / API References](https://web3auth.io/docs/sdk) | ||
- [Pricing](https://web3auth.io/pricing.html) | ||
- [Community Portal](https://community.web3auth.io) | ||
- [React Native Website](https://reactnative.dev) - learn more about React Native. | ||
- [Getting Started](https://reactnative.dev/docs/environment-setup) - an **overview** of React Native and how setup your environment. | ||
- [Learn the Basics](https://reactnative.dev/docs/getting-started) - a **guided tour** of the React Native **basics**. | ||
- [Blog](https://reactnative.dev/blog) - read the latest official React Native **Blog** posts. | ||
- [`@facebook/react-native`](https://github.com/facebook/react-native) - the Open Source; GitHub **repository** for React Native. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.