Creating a UserSession using app private key

The blockstack.js documentation (https://blockstack.github.io/blockstack.js/classes/usersession.html) mentions that a user can directly sign in by providing the app private key .

Is there an example that demonstrates this?

This would be useful for writing unit tests.

2 Likes

I think it refers to the fact that you can create a new UserSession object directly and start using that within your code. Will let @zone117x chime in.

1 Like

If you are just starting out, I would suggest you get an overview of how are Auth works here:

A detailed walk through with code examples here:

And any of our all of our sample code does some form of auth but the easiest is Hello Blockstack here:

1 Like

Thanks @moxiegirl I’m familiar with how the auth process generally works with the blockstack browser.

My question is more to do with how we can create a valid UserSession without having to open a browser. This would especially be usefull for writing automated unit tests.

@clydedcruz.id.blocks thanks for clarifying your question. @timstackblock Is our test engineer, he might be best to answer that.

This should be the minimum required code to create a UserSession instance that runs in Node.js, and can perform gaia file operations.

const appConfig = new AppConfig(
  ['store_write'], 
  'helloblockstack.com' /* your app origin */ 
)
const dataStore = new InstanceDataStore({ 
  userData: {
    appPrivateKey: '8a2c9a65...', /* A user's app private key */
    hubUrl: 'https://hub.blockstack.org' /* A user's gaia hub server */
    /* The rest of the properties can be null */
  }
})
const userSession = new UserSession({
  appConfig: appConfig,
  sessionStore: dataStore
})

Note: InstanceDataStore is not exported to the top level module. Use import { InstanceDataStore } from 'blockstack/lib/auth/sessionStore'

6 Likes

Thanks! Exactly what I was looking for :slight_smile:

1 Like

Hey @zone117x. How were you able to import InstanceDataStore? I don’t see that exposed anywhere in the API. I do however see SessionDataStore in the documentation, but it does not seem to be exposed either, even though the documentation claims that it should be.

1 Like

InstanceDataStore is not hoisted as a top level export. It can be imported with

import { InstanceDataStore } from 'blockstack/lib/auth/sessionStore'

I updated the original comment with a notice.

Thanks! That does the trick!