Authenticate a 3Box

First, authenticate a user's 3Box.

To initialize a new 3Box session, call Box.openBox().

If the user already has a 3Box account and profile, your application will gain access to it; if the user does not have an existing 3Box account, this method will automatically create one for them.

const box = await Box.openBox('0x12345...abcde', ethereumProvider)

To call this method you need the users ethereum address (first parameter) and an ethereum provider (second parameter). You can get the ethereum address by calling window.ethereum.enable() or window.web3.eth.getAccounts(), and the ethereum provider is either window.ethereum or window.web3.currentProvider.

When called, this method will cause your user's Ethereum wallet provider to present the user with a message to sign: This app wants to view and update your 3Box profile. It will look something like this:

Once approved, you will be able to update, decrypt, and interact with your user's 3Box profile store.

Box.openBoxonly needs to be called once in a user flow.

Then, sync data from the network.

When you first authenticate the user's 3Box, all data might not be synced from the network yet. Therefore, you should wait for the data to be synced using the box.syncDone property. This will allow you to know when the user's current data is available to you.

await box.syncDone

We advise against setting any new data before this sync has happened. However reading data before the sync is complete is fine and encouraged - just remember to check for updates once this callback is fired!