Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Blockstack Auth

Quickstart

  1. Install blockstack.js:
npm install blockstack --save
  1. Import Blockstack into your project
import blockstack from 'blockstack'
  1. Wire up a sign in button
document.getElementById('signin-button').addEventListener('click', function() {
  var authRequest = blockstack.makeAuthRequest(null, window.location.origin)
  blockstack.redirectUserToSignIn(authRequest)
})
  1. Wire up a sign out button
document.getElementById('signout-button').addEventListener('click', function() {
  blockstack.signUserOut(window.location.origin)
})
  1. Include the logic to (a) load user data (b) handle the auth response
function showProfile(profile) {
  var person = new blockstack.Person(profile)
  document.getElementById('heading-name').innerHTML = person.name()
  document.getElementById('avatar-image').setAttribute('src', person.avatarUrl())
  document.getElementById('section-1').style.display = 'none'
  document.getElementById('section-2').style.display = 'block'
}

if (blockstack.isUserSignedIn()) {
  blockstack.loadUserData(function(userData) {
    showProfile(userData.profile)
  })
} else if (blockstack.isSignInPending()) {
  blockstack.signUserIn(function(userData) {
    window.location = window.location.origin
  })
}
  1. Create a manifest.json file
{
  "name": "Hello, Blockstack",
  "start_url": "localhost:5000",
  "description": "A simple demo of Blockstack Auth",
  "icons": [{
    "src": "https://helloblockstack.com/icon-192x192.png",
    "sizes": "192x192",
    "type": "image/png"
  }]
}
  1. Serve your application

Operations

Request a user to sign in

import { requestSignIn } from 'blockstack'

const authRequest = blockstack.makeAuthRequest(null, window.location.origin)
blockstack.redirectUserToSignIn(authRequest)

Sign a user in

import { signUserIn } from 'blockstack'

signUserIn((userData) => {
    // Redirect the user to the home page
})

Create a raw auth request

import { makeAuthRequest, makeECPrivateKey } from 'blockstack'

const privateKey = makeECPrivateKey()
const authRequest = makeAuthRequest(privateKey)

Verify an auth request

import { verifyAuthRequest } from 'blockstack'

const verified = verifyAuthRequest(authRequest)

Create an auth response

import { makeAuthResponse, makeECPrivateKey } from 'blockstack'

const privateKey = makeECPrivateKey()
const profile = { "name": "Naval Ravikant" }
const username = "naval.id"
const authResponse = makeAuthResponse(privateKey, profile, username)

Verify an auth response

import { verifyAuthResponse } from 'blockstack'

const verified = verifyAuthResponse(authResponse)

Examples