Authenticate a Space

First, authenticate a user's space.

To interact with a user's space, call box.openSpace().

If the user already has a space with this name, your application will gain access to it; if the user does not have the space, this method will automatically create one for them.

const space = await box.openSpace('myApp')

When called, this method will cause your user's Ethereum wallet provider to present the user with a message to sign: Allow this app to open your myApp space. It will look something like this:

Once approved, you will be able to update, decrypt, and interact with your user's space.

Want to authenticate multiple spaces? You will need to request each space authentication individually, and the user will need to approve each request individually in their wallet.

Then, sync data from the network.

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

await space.syncDone

We advise against setting any 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!

An alternative way to authenticate a space

Instead of using openBox and openSpace users can use the following flow:

const box = await Box.create(window.ethereum)
const spaces = [ 'space1', 'space2', 'space3']
await box.auth(spaces, { address })
await box.openSpace('space1')

This approach uses Box.create to create an instance of 3Box. This can be done as soon as the page loads. Then box.auth which allows authentication with multiple spaces is called. Finally openSpace is used to open individual spaces before interacting with them. This approach can be faster, as the 3Box instance can be created earlier, and multiple spaces can be authenticated at once.