@arcblock/did-connect

2.10.59 • Public • Published

@arcblock/did-connect

Client side library to work with DID Connect by ArcBlock.

Usage

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

<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"
    ...
  />
)}

display DidConnect in popup

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

<DidAvatar did={userDid} size={256} />

DidButton

<ConnectButton size="large" />
<ConnectButton size="medium" />
<ConnectButton size="small" />
<ConnectButton>Custom Text</ConnectButton>

DidLogo

<DidLogo size={32} />

DidAddress

<DidAddress did={userDid} size={32} />

SessionManager

<SessionProvider serviceHost={get(window, 'blocklet.prefix', '/')} webWalletUrl={webWalletUrl}>
  <SessionConsumer>{(value) => <SessionManager session={value.session} />}</SessionConsumer>
  ...
</SessionProvider>

Package Sidebar

Install

npm i @arcblock/did-connect

Weekly Downloads

2,617

Version

2.10.59

License

Apache-2.0

Unpacked Size

1.16 MB

Total Files

136

Last publish

Collaborators

  • wangshijun
  • polunzh
  • mave99a
  • gxw