oreid-webpopup
v2.4.0
Published
Popup Web User Experience for ORE ID
Downloads
105
Readme
ORE ID Web Popup
Popup Web User Experience for ORE ID
This library can be used in any web application. However, if you are using React, you should use the React alternative to this library instead oreid-react
.
Overview
This library works with oreid-js to provide a web pop-up user experince for common ORE ID flows - like logging-in and signing a transaction
Start a flow by triggering the user popup:
oreid.popup.auth({
provider: 'google'
})
.then(onSuccess)
.catch(onError);
This will launch a pop-up. When the user finishes the flow, the onError or onSuccess callback will be called.
This library works with oreid-js to save a users' accessToken after login to LocalStorage as well as perform other housekeeping functions.
Important
- This library must be used within browser or a web wrapper component that has a
window
object - The auth login flow should be triggered by the user clicking on a button, link, or some other item that causes an Event. This will help get around pop-up blockers
Installation
npm install oreid-js oreid-webpopup
or
yarn add oreid-js oreid-webpopup
Sample Code
Initalize
import { OreId } from 'oreid-js'
import { WebPopup } from 'oreid-webpopup'
// Initialize libraries
const oreId = new OreId({ appId, apiKey, plugins:{ popup: WebPopup() }});
oreId.init().then(
// oreid is ready
)
Auth
import { oreId, isInitialized } from "./bootstrap";
<script>
const onClick = () => {
oreid.popup.auth({
provider: 'google'
})
.then(data => { console.log(data) })
.catch(error => { console.log(error) });
}
</script>
<button click="onClick()">Auth</button>
Sign
import { oreId, isInitialized } from "./bootstrap";
<script>
const onClick = () => {
const userChainAccounts = oreId.auth.user.data.chainAccounts;
// get first Ethereum account in user’s OREID account
const ethAccount = userChainAccounts.find(ca => ca.chainNetwork === 'eth_main')
// transactionBody is blockchain transaction (differs by chain type)
const transactionBody = {
from: "0xF478...",
to: "0xA200...",
value: "1" // always a string
};
oreId.createTransaction({
transaction: transactionBody,
chainAccount: ethAccount.chainAccount,
chainNetwork: ethAccount.chainNetwork,
}).then(transaction => {
// have the user approve signature
oreId.popup.sign({ transaction })
.then({ transactionId } => { ... })
.catch(onError);
})
}
</script>
<button click="onClick()">Sign Transaction</button>
Create New Chain Account
import { oreId, isInitialized } from "./bootstrap";
<script>
const onClick = () => {
// have the user approve signature
oreId.popup.newChainAccount({
chainNetwork: 'eos-kylin',
})
.then({ chainAccount } => { ... })
.catch(onError);
}
</script>
<button click="onClick()">Create New Chain Account</button>
Recomended Startup Pattern
// bootstrap.ts
import { OreId } from "oreid-js";
import { WebPopup } from "oreid-webpopup";
export let oreId: OreId;
export let isInitialized: boolean = false;
const appId = "MY_APP_ID";
// Initialize libraries
() => {
if(initialized) return;
oreId = new OreId({ appId, plugins:{ popup: WebPopup() }}); // apiKey is required for some features
oreId.init().then(() => {
// webpopup is ready to use
isInitialized = true
})
}();