Browse Source

feat: auth tutorial updates for stacks.js

feat/stacks-js-updates
Ken 4 years ago
parent
commit
674abd7ffe
  1. 58
      src/pages/authentication/building-todo-app.md
  2. 53
      src/pages/authentication/connect.md

58
src/pages/authentication/building-todo-app.md

@ -16,7 +16,7 @@ images:
## Introduction
In this tutorial, you will learn about Blockstack authentication and storage by installing,
In this tutorial, you will learn about Stacks authentication and storage by installing,
running and reviewing the code for a "Todos" web app built with Blockstack and [React](https://reactjs.org/).
This app highlights the following platform functionality:
@ -68,15 +68,15 @@ You should see the app's landing page:
![The homepage of the todos app](/images/todos-home.png)
## Onboard into your first Blockstack app
## Onboard into your first Stacks app
### Step 1: Choose **Get started** to start onboarding into the app.
The app displays a standardized introductory modal using
[Blockstack Connect](https://github.com/blockstack/ux/tree/master/packages/connect), a JavaScript
library that makes it easy to integrate Blockstack into the UI of any web app.
[Stacks Connect](https://github.com/blockstack/ux/tree/master/packages/connect), a JavaScript
library that makes it easy to integrate Stacks authentication into the UI of any web app.
![The Blockstack Connect Modal](/images/todos-intro.png)
![The Stacks Connect Modal](/images/todos-intro.png)
Below, you can see the relevant parts of the [React component](https://reactjs.org/docs/react-component.html)
that triggers this modal in [`src/components/Signin.jsx`](https://github.com/blockstack/blockstack-todos/blob/master/src/components/Signin.jsx):
@ -84,7 +84,7 @@ that triggers this modal in [`src/components/Signin.jsx`](https://github.com/blo
```js
// src/components/Signin.jsx
import { useConnect } from '@blockstack/connect';
import { useConnect } from '@stacks/connect';
export const Signin = () => {
const { doOpenAuth } = useConnect();
@ -95,14 +95,14 @@ export const Signin = () => {
This component imports the [React hook](https://reactjs.org/docs/hooks-overview.html)
[`useConnect`](https://github.com/blockstack/ux/blob/master/packages/connect/src/react/hooks/use-connect.ts)
from the Blockstack Connect library.
from the Stacks Connect library.
`useConnect` returns many helper functions such as
[`doOpenAuth`](https://github.com/blockstack/ux/blob/master/packages/connect/src/react/hooks/use-connect.ts#L33),
which triggers this modal upon click of the "Get started" button.
The modal is designed to prepare new users for a different type of relationship with
Blockstack apps, one in which they authenticate with a _Secret Key_ that's used to encrypt
Stacks apps, one in which they authenticate with a _Secret Key_ that's used to encrypt
their private data.
The modal displays the app's name and icon as configured in
@ -119,12 +119,12 @@ appDetails: {
```
This component loads the [`UserSession`](https://blockstack.github.io/blockstack.js/classes/usersession.html)
module from a second Blockstack library called [blockstack.js](https://github.com/blockstack/blockstack.js/),
which complements Blockstack Connect by providing an API for many protocol-level operations, such as for
module from a second Stacks library called [@stacks/auth](https://github.com/blockstack/blockstack.js/),
which complements Stacks Connect by providing an API for many protocol-level operations, such as for
authentication and storage.
```js
import { UserSession } from 'blockstack';
import { UserSession } from '@stacks/auth';
import { appConfig } from '../assets/constants';
// ...
@ -145,7 +145,7 @@ export const appConfig = new AppConfig(['store_write', 'publish_data']);
The `appDetails` and `userSession` objects are joined by the callback function
[`finished`](https://github.com/blockstack/blockstack-todos/blob/master/src/components/App.jsx#L31)
in configuring Blockstack Connect for authentication with the `authOptions` object:
in configuring Stacks Connect for authentication with the `authOptions` object:
```js
// src/components/App.jsx
@ -179,7 +179,7 @@ componentDidMount() {
### Step 2: Choose **Get started** to generate a _Secret Key_.
The app triggers a popup window in which [the Blockstack App](https://github.com/blockstack/ux/tree/master/packages/app)
The app triggers a popup window in which [the Stacks App](https://github.com/blockstack/ux/tree/master/packages/app)
is loaded from [`app.blockstack.org`](http://app.blockstack.org/) and begins generating a new _Secret Key_.
![What the UI looks like when a new ID is generated](/images/todos-generation.svg)
@ -187,8 +187,8 @@ is loaded from [`app.blockstack.org`](http://app.blockstack.org/) and begins gen
### Step 3: Choose **Copy Secret Key** to copy your _Secret Key_ to the clipboard.
The _Secret Key_ is a unique 12-word [mnemonic phrase](https://en.bitcoinwiki.org/wiki/Mnemonic_phrase) that
empowers the user not only to access Blockstack apps securely and independently. It's also used to encrypt
all of the private data they create and manage with Blockstack apps.
empowers the user not only to access Stacks apps securely and independently. It's also used to encrypt
all of the private data they create and manage with Stacks apps.
_Secret Keys_ are like strong passwords. However, they can never be recovered if lost or reset if stolen.
As such, it's paramount that users handle them with great care.
@ -203,7 +203,7 @@ As such, it's paramount that users handle them with great care.
The username will be used by the app to generate a URL for sharing your todos, should you choose to make them public.
It is registered on the Stacks blockchain with the [Blockstack Naming System (BNS)](/core/naming/introduction)
It is registered on the Stacks blockchain with the [Stacks Naming System (SNS)](/core/naming/introduction)
and associated with your _Secret Key_.
![Choosing a user name example](/images/todos-username.svg)
@ -222,8 +222,11 @@ The data for all todos are saved as JSON to the Gaia hub linked to your Secret K
[`src/assets/data-store.js`](https://github.com/blockstack/blockstack-todos/blob/master/src/assets/data-store.js#L26) module:
```jsx
import { Storage } from '@stacks/storage';
export const saveTasks = async (userSession, tasks, isPublic) => {
await userSession.putFile(TASKS_FILENAME, JSON.stringify({ tasks, isPublic }), {
const storage = new Storage(userSession);
await storage.putFile(TASKS_FILENAME, JSON.stringify({ tasks, isPublic }), {
encrypt: !isPublic,
});
};
@ -233,8 +236,11 @@ These todos are subsequently loaded using the [`getFile`](http://blockstack.gith
method of the same object in the same module:
```jsx
import { Storage } from '@stacks/storage';
export const fetchTasks = async (userSession, username) => {
const tasksJSON = await userSession.getFile(TASKS_FILENAME, {
const storage = new Storage(userSession);
const tasksJSON = await storage.getFile(TASKS_FILENAME, {
decrypt: false,
username: username || undefined,
});
@ -255,11 +261,11 @@ Select "Make public" to make your todos accessible to the public for sharing via
This will call `saveTasks` with the `isPublic` parameter set to `true`, which is used to disable encryption when using `putFile`.
The app will now show all of your todos to anyone who visits the URL displayed with your Blockstack username as a suffix.
The app will now show all of your todos to anyone who visits the URL displayed with your Stacks username as a suffix.
## Sign out and see your public tasks
Select "Sign out" to deauthenticate the app with your Blockstack account.
Select "Sign out" to deauthenticate the app with your Stacks account.
This triggers an event, which
[under the hood](https://github.com/blockstack/blockstack-todos/blob/master/src/components/Header.jsx#L47)
@ -274,16 +280,16 @@ option is then passed to `getFile`, which will lookup where that user's tasks ar
## Sign back in
At this point, you will be logged out from the app but not you'll still have an active session with the Blockstack
app itself on [app.blockstack.org](https://app.blockstack.org). Navigate to app.blockstack.org and select "Sign out" there if you want to deauthenticate the Blockstack app as well.
At this point, you will be logged out from the app but not you'll still have an active session with the Stacks
app itself on [app.blockstack.org](https://app.blockstack.org). Navigate to app.blockstack.org and select "Sign out" there if you want to deauthenticate the Stacks app as well.
Once signed out, select "Sign in" to sign back in with your _Secret Key_.
If you've previously deauthenticated the Blockstack app, you'll see a prompt to enter your _Secret Key_:
If you've previously deauthenticated the Stacks app, you'll see a prompt to enter your _Secret Key_:
![An example of a sign in screen](/images/todos-sign-in.svg)
The above screen will be ommitted if you have an active session with the Blockstack app already.
The above screen will be ommitted if you have an active session with the Stacks app already.
Then you'll be presented with the option to select an existing username associated with your _Secret Key_ or
create a new one if you wish to authenticate the app with a different identity and data set:
@ -294,6 +300,6 @@ You'll now see your todos as an authenticated user for the username you've chose
## Learn more
Read [the Blockstack Connect guide](/authentication/connect) and
[the blockstack.js reference](https://blockstack.github.io/blockstack.js/) to learn more about the
Read [the Stacks Connect guide](/authentication/connect) and
[the stacks.js reference](https://blockstack.github.io/blockstack.js/) to learn more about the
libraries used in this tutorial.

53
src/pages/authentication/connect.md

@ -1,5 +1,5 @@
---
title: Blockstack Connect
title: Stacks Connect
description: Learn what Connect is and how to integrate it into an app.
experience: beginners
duration: 15 minutes
@ -10,34 +10,34 @@ images:
## Introduction
Blockstack Connect is a JavaScript library for integrating Blockstack authentication, data storage, and smart contracts into your app.
Stacks Connect is a JavaScript library for integrating Stacks authentication, data storage, and smart contracts into your app.
The library empowers you to:
- Register new users with a pre-built onboarding flow that quickly educates them as to the privacy benefits of using your app with Blockstack and provisions a "Secret Key" that secures their identity and data against the Stacks blockchain.
- Register new users with a pre-built onboarding flow that quickly educates them as to the privacy benefits of using your app with Stacks authentication and provisions a "Secret Key" that secures their identity and data against the Stacks blockchain.
- Authenticate users when they return to your app using that same Secret Key.
- Prompt users to sign transactions with smart contracts as written in Clarity and published to the Stacks blockchain.
## How does this compare to `blockstack.js`?
## How does this compare to `stacks.js`?
Although [`blockstack.js`](https://github.com/blockstack/blockstack.js) can also be used to authenticate users, it implements the deprecated [Blockstack Browser](https://browser.blockstack.org/) and lacks any pre-built onboarding UI that educates users as to how your app is more secure for having implemented Blockstack. As such, we advise that you use `blockstack.js` for all other functionality apart from authentication, such as saving and retrieving user data with Gaia.
Connect uses stacks.js under the hood to construct and decode authentication requests. It provides pre-built onboarding UI that educates users as to how your app is more secure for having implemented Stacks authentication. You will need to use `stack.js` packages for storage and transactions.
## Start building with Blockstack Connect
## Start building with Stacks Connect
Head over to the [to-do app tutorial](/authentication/building-todo-app) to learn how to build apps with Blockstack Connect. For interaction with Stacks accounts and smart contracts with Blockstack Connect see the [transaction sigining section](/smart-contracts/signing-transactions).
Head over to the [to-do app tutorial](/authentication/building-todo-app) to learn how to build apps with Stacks Connect. For interaction with Stacks accounts and smart contracts with Stacks Connect see the [transaction sigining section](/smart-contracts/signing-transactions).
## Installation
With yarn:
```bash
yarn add @blockstack/connect
yarn add @stacks/connect
```
With npm:
```bash
npm install --save @blockstack/connect
npm install --save @stacks/connect
```
## Usage
@ -68,6 +68,7 @@ export interface AuthOptions {
| finished | function | | false | A callback that can be invoked after authentication. This prevents having to do a whole page refresh in a new tab. One argument is passed to this callback, which is an object with `userSession` included. If included, then the `redirectTo` path is ignored, and the user will be logged in automatically. |
| sendToSignIn | boolean | false | true | Whether the user should go straight to the 'sign in' flow (false) or be presented with the 'sign up' flow (true) instead. |
| userSession | UserSession | | true | pass a `UserSession` instance to use for authentication. If it's not passed, `@blockstack/connect` will create one for you. |
| userSession | UserSession | | false | pass a `UserSession` instance to use for authentication. If it's not passed, `@stacks/connect` will create one for you. |
### In React Apps
@ -76,7 +77,7 @@ If you're using `connect` in a React app, then the best option is to include `co
First, setup the `Connect` provider at the "top-level" of your app - probably next to wherever you would put a Redux provider, for example.
```jsx
import { Connect } from '@blockstack/connect';
import { Connect } from '@stacks/connect';
const authOptions = {
redirectTo: '/',
@ -95,7 +96,7 @@ const App = () => <Connect authOptions={authOptions}>// the rest of your app's c
Later, when you want to begin the onboarding process, use the `useConnect` hook to get `connect`'s `doOpenAuth` method.
```jsx
import { useConnect } from '@blockstack/connect';
import { useConnect } from '@stacks/connect';
const SignInButton = () => {
const { doOpenAuth } = useConnect();
@ -110,15 +111,15 @@ To send the user straight to sign in, call `doOpenAuth(true)`.
### In ES6 (non-React) apps
If you aren't using React, or just want a simpler API, then you can use the `showBlockstackConnect` method.
If you aren't using React, or just want a simpler API, then you can use the `showStacksConnect` method.
```jsx
import { showBlockstackConnect } from '@blockstack/connect';
import { showStacksConnect } from '@stacks/connect';
const authOptions = {
/** See docs above for options */
};
showBlockstackConnect(authOptions);
showStacksConnect(authOptions);
```
#### Sign In
@ -127,27 +128,27 @@ To send the user straight to sign in, include `sendToSignIn: true` in your `auth
#### Note about dependency size:
If you're building a non-React app, note that importing `@blockstack/connect` will add React dependencies to your JavaScript bundle. We recommend using something like [Webpack resolve aliases](https://webpack.js.org/configuration/resolve/) to replace `react` with `preact` in production, which reduces your bundle size. Check out [our own webpack.config.js file](https://github.com/blockstack/ux/blob/master/packages/connect/webpack.config.js#L87:L95) to see how we use this for production builds.
If you're building a non-React app, note that importing `@stacks/connect` will add React dependencies to your JavaScript bundle. We recommend using something like [Webpack resolve aliases](https://webpack.js.org/configuration/resolve/) to replace `react` with `preact` in production, which reduces your bundle size. Check out [our own webpack.config.js file](https://github.com/blockstack/ux/blob/master/packages/connect/webpack.config.js#L87:L95) to see how we use this for production builds.
If you're using the hosted version of `@blockstack/connect` (described below), then you already have a production-optimized bundle.
If you're using the hosted version of `@stacks/connect` (described below), then you already have a production-optimized bundle.
### Using a hosted version of `@blockstack/connect`
### Using a hosted version of `@stacks/connect`
If you aren't using ES6 imports, you can still use `connect`! We package the library so that it can be automatically used with [unpkg](https://unpkg.com/).
First, include the script in your HTML:
```html
<script src="https://unpkg.com/@blockstack/connect" />
<script src="https://unpkg.com/@stacks/connect" />
```
Then, you can use API methods under the `blockstackConnect` global variable:
Then, you can use API methods under the `stacksConnect` global variable:
```jsx
const authOptions = {
/** See docs above for options */
};
blockstackConnect.showBlockstackConnect(authOptions);
stacksConnect.showStacksConnect(authOptions);
```
## Handling redirect fallbacks
@ -158,7 +159,7 @@ To make sure your app handles this gracefully, you'll need to handle the case wh
`${authOptions.redirectTo}?authResponse=....`
To finalize authentication with this flow, you'll need to utilize the `UserSession` methods `isSignInPending()` and `handlePendingSignIn()`. For more information, check out the [blockstack.js API reference](https://blockstack.github.io/blockstack.js/).
To finalize authentication with this flow, you'll need to utilize the `UserSession` methods `isSignInPending()` and `handlePendingSignIn()`. For more information, check out the [stacks.js API reference](https://blockstack.github.io/blockstack.js/).
```js
const userSession = new UserSession(appConfig);
@ -172,20 +173,20 @@ if (userSession.isSignInPending()) {
## Design Guidance
Blockstack is valuable to users, but it can also be a barrier to those unfamiliar with Blockstack. The following guidelines serve to remedy that and help you onboard as many new users as you can.
Stacks authentication is valuable to users, but it can also be a barrier to those unfamiliar with Stacks. The following guidelines serve to remedy that and help you onboard as many new users as you can.
### Delay Blockstack onboarding as long as possible
### Delay Stacks onboarding as long as possible
People will often leave apps when things are asked of them before they experience the app. Give them a chance to try your app before you ask them to sign up with Blockstack. For example, a note taking app could let a new user write a couple of notes before prompting them to save their progress.
People will often leave apps when things are asked of them before they experience the app. Give them a chance to try your app before you ask them to sign up with Stacks authentication. For example, a note taking app could let a new user write a couple of notes before prompting them to save their progress.
### Provide an easy way in for new users
Many new users to your app will not be familiar with Blockstack yet and will be hesitant to click a Blockstack-branded button. Provide a generic button for users that are new to your app and Blockstack. Blockstack Connect will introduce new users to Blockstack and recognize existing users.
Many new users to your app will not be familiar with Stacks authentication yet and will be hesitant to click a Stacks-branded button. Provide a generic button for users that are new to your app and Stacks. Stacks Connect will introduce new users to Stacks authentication and recognize existing users.
![Design Guidance Example](/images/connect-call-to-action-branding.png)
### Provide a quick way for existing users to sign in
You can point users to a specific part of the Blockstack App. For instance, a “Sign in” button on your website can redirect users to the sign in flow of the Blockstack App. If you do this, make sure you also have an option that is explicitly for new users and that points to the sign up flow.
You can point users to a specific part of the Stacks App. For instance, a “Sign in” button on your website can redirect users to the sign in flow of the Stacks App. If you do this, make sure you also have an option that is explicitly for new users and that points to the sign up flow.
To implement this functionality, check out our section on sending users to sign in immediately.

Loading…
Cancel
Save