Install blockstack.js:
npm install blockstack --save
Import Blockstack into your project
import blockstack from 'blockstack'
Wire up a sign in button
document . getElementById ( 'signin-button' ) . addEventListener ( 'click' , function ( ) {
var authRequest = blockstack . makeAuthRequest ( null , window . location . origin )
blockstack . redirectUserToSignIn ( authRequest )
} )
Wire up a sign out button
document . getElementById ( 'signout-button' ) . addEventListener ( 'click' , function ( ) {
blockstack . signUserOut ( window . location . origin )
} )
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
} )
}
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"
}]
}
Serve your application
Request a user to sign in
import { requestSignIn } from 'blockstack'
const authRequest = blockstack . makeAuthRequest ( null , window . location . origin )
blockstack . redirectUserToSignIn ( authRequest )
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 )
import { verifyAuthRequest } from 'blockstack'
const verified = verifyAuthRequest ( authRequest )
import { makeAuthResponse , makeECPrivateKey } from 'blockstack'
const privateKey = makeECPrivateKey ( )
const profile = { "name" : "Naval Ravikant" }
const username = "naval.id"
const authResponse = makeAuthResponse ( privateKey , profile , username )
import { verifyAuthResponse } from 'blockstack'
const verified = verifyAuthResponse(authResponse)