Client side library to work with DID Connect by ArcBlock.
yarn add @arcblock/did-connect
Then:
import axios from 'axios';
// import each module individually
import DidAddress from '@arcblock/did-connect/lib/Address';
import DidConnect from '@arcblock/did-connect/lib/Connect';
import DidAvatar from '@arcblock/did-connect/lib/Avatar';
import DidButton from '@arcblock/did-connect/lib/Button';
import DidLogo from '@arcblock/did-connect/lib/Logo';
import { SessionProvider, SessionConsumer } from '@arcblock/did-connect/lib/Session';
import SessionManager from '@arcblock/did-connect/lib/SessionManager';
// or use ES6 named imports
import {
Address as DidAddress,
Connect as DidConnect,
Avatar as DidAvatar,
Button as DidButton,
Logo as DidLogo,
Session,
SessionManager,
} from '@arcblock/did-connect/lib';
<DidConnect
popup
open={open}
action="login"
checkFn={axios.get}
onClose={() => handleOnClose()}
onSuccess={() => (window.location.href = '/profile')}
messages={{
title: 'login',
scan: 'Scan QR code with DID Wallet',
confirm: 'Confirm login on your DID Wallet',
success: 'You have successfully signed in!',
}}
/>
Note: DidConnect component has a built-in instance of WebWalletSWKeeper that embeds a wallet iframe in the DOM to keep the service worker of the web wallet alive. When the DidConnect component is destroyed, WebWalletSWKeeper will also be destroyed, so avoid using it like the following:
{open && (
<DidConnect
popup
action="login"
...
/>
)}
const [open, setOpen] = React.useState(false);
const handleOnClose = () => {
// ...
setOpen(false);
};
...
<button type="button" onClick={() => setOpen(true)}>
Open DidConnect
</button>
<DidConnect
popup
open={open}
action="login"
checkFn={axios.get}
onClose={() => handleOnClose()}
onSuccess={() => (window.location.href = '/profile')}
messages={{
title: 'login',
scan: 'Scan QR code with DID Wallet',
confirm: 'Confirm login on your DID Wallet',
success: 'You have successfully signed in!',
}}
webWalletUrl={webWalletUrl}
/>
<DidAvatar did={userDid} size={256} />
<ConnectButton size="large" />
<ConnectButton size="medium" />
<ConnectButton size="small" />
<ConnectButton>Custom Text</ConnectButton>
<DidLogo size={32} />
<DidAddress did={userDid} size={32} />
<SessionProvider serviceHost={get(window, 'blocklet.prefix', '/')} webWalletUrl={webWalletUrl}>
<SessionConsumer>{(value) => <SessionManager session={value.session} />}</SessionConsumer>
...
</SessionProvider>