Creating a UserSession using app private key

#1

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
#2

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
#3

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
#4

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.

#5

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

#6

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'

5 Likes
Support for UserSession in node environment
#7

Thanks! Exactly what I was looking for :slight_smile:

1 Like
#8

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
#9

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.

#10

Thanks! That does the trick!