Skip to content

Commit 4eacefd

Browse files
authored
Merge pull request #40 from storyblok/docs/readme
docs: add migration section for v2
2 parents 4c3c306 + bd06b94 commit 4eacefd

File tree

1 file changed

+79
-49
lines changed

1 file changed

+79
-49
lines changed

README.md

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,65 @@
22

33
A JavaScript library for managing authentication for [Storyblok](https://www.storyblok.com) apps.
44

5+
## Migrating v1 to v2
6+
7+
The `@storyblok/app-extension-auth` v1 stored the access token in a cookie. However, in a future version of v2, the library will offer an adapter pattern, allowing you to store the access token elsewhere, such as in a dedicated database. The following breaking changes abstract the code to make this transition easier in the near future:
8+
9+
### `sessionCookieStore``getSessionStore`
10+
11+
```js
12+
// from
13+
import { sessionCookieStore } from '@storyblok/app-extension-auth'
14+
15+
// to
16+
import { getSessionStore } from '@storyblok/app-extension-auth'
17+
```
18+
19+
### `getSessionStore`
20+
21+
```js
22+
const sessionStore = getSessionStore(authHandlerParams)({
23+
req: event.node.req,
24+
res: event.node.res,
25+
})
26+
```
27+
28+
The `sessionStore` created by `sessionCookieStore` now has three methods: `get`, `put`, and `remove`. The `getAll` method is no longer provided. If you believe the `getAll` method is still useful for your use case, please let us know by opening a GitHub issue.
29+
30+
The `put` and `remove` methods now return a `Promise<boolean>` instead of a `Promise<void>`.
31+
32+
### `AuthHandlerParams`
33+
34+
```js
35+
import { authHandler } from '@storyblok/app-extension-auth'
36+
const params: AuthHandlerParams = {
37+
// ...
38+
}
39+
authHandler(params)
40+
```
41+
42+
The parameter `AuthHandlerParams['cookieName']` has been renamed to `AuthHandlerParams['sessionKey']`.
43+
544
## Getting Started
645

746
See our starters:
847

9-
* [Next.js](https://github.com/storyblok/custom-app-examples/tree/main/app-nextjs-starter)
48+
- [Next.js](https://github.com/storyblok/custom-app-examples/tree/main/app-nextjs-starter)
1049

1150
### Install the app
1251

1352
Install with npm:
1453

1554
```shell
16-
npm install --save-exact @storyblok/app-extension-auth
55+
npm install @storyblok/app-extension-auth
1756
```
1857

1958
or with Yarn:
2059

2160
```shell
22-
yarn add --exact @storyblok/app-extension-auth
61+
yarn add @storyblok/app-extension-auth
2362
```
2463

25-
Note: the `@storyblok/app-extension-auth` is currently in alpha, and is prone to changes. Therefore, save the [exact version](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#dependencies) to your `package.json` with the commands above.
26-
2764
### Set a URL
2865

2966
Decide a URL for your app. As a first step, this should be a URL for local development. For this you will need a secure tunnel, for example [ngrok](https://ngrok.com/).
@@ -38,11 +75,11 @@ ngrok http 3000
3875

3976
Create an App in Storyblok's Partner Portal. Then open app's settings, navigate to _Oauth 2_, and configure the following values:
4077

41-
* **URL to your app**: the index page of your app. For example, `https://my-app.com/`.
78+
- **URL to your app**: the index page of your app. For example, `https://my-app.com/`.
4279

43-
* **OAuth2 callback URL**: the api endpoint that will initiate the OAuth flow.
44-
* Calculated as: `{baseUrl}/{endpointPrefix}/storyblok/callback`
45-
* Example value: `https://my-app.com/api/connect/storyblok/callback`
80+
- **OAuth2 callback URL**: the api endpoint that will initiate the OAuth flow.
81+
- Calculated as: `{baseUrl}/{endpointPrefix}/storyblok/callback`
82+
- Example value: `https://my-app.com/api/connect/storyblok/callback`
4683

4784
Substitute `{baseUrl}` and `{endpointPrefix}` for your own values. These parameters will be referenced again in your code; see the next section.
4885

@@ -54,43 +91,45 @@ In your source code, create the following object (you will need it later):
5491
import { AuthHandlerParams } from '@storyblok/app-extension-auth'
5592

5693
export const params: AuthHandlerParams = {
57-
clientId: process.env.APP_CLIENT_ID,
94+
clientId: process.env.APP_CLIENT_ID,
5895
clientSecret: process.env.APP_CLIENT_SECRET,
59-
baseUrl: process.env.APP_URL,
96+
baseUrl: process.env.APP_URL,
6097
successCallback: '/',
6198
errorCallback: '/401',
62-
endpointPrefix: '/api/connect',
99+
endpointPrefix: '/api/connect',
63100
}
64101
```
65102

66103
Some variables should be loaded via environmental variables (`.env.local`):
67104

68-
* `clientId` -- The client ID is a public identifier for your apps. Find the Client ID in the app settings on Storyblok.
69-
* `clientSecret` -- The client secret is a secret known only to the application and the authorization server. Find the client secret in the app settings on Storyblok.
70-
71-
Load it into the application as an environmental variable.
72-
It must be kept confidential.
73-
* `baseUrl` -- The base URL specifies the base URL to use for all relative authentication API endpoints created by authHandler().
105+
- `clientId` -- The client ID is a public identifier for your apps. Find the Client ID in the app settings on Storyblok.
106+
- `clientSecret` -- The client secret is a secret known only to the application and the authorization server. Find the client secret in the app settings on Storyblok.
107+
Load it into the application as an environmental variable.
108+
It must be kept confidential.
109+
- `baseUrl` -- The base URL specifies the base URL to use for all relative authentication API endpoints created by authHandler().
74110
The base URL must be absolute and secure with https.
75-
111+
76112
For example, the base URL `https://my-app.my-domain.com/` will create the following api endpoints:
113+
77114
- `https://my-app.my-domain.com/storyblok` for initiating the authentication flow
78115
- `https://my-app.my-domain.com/storyblok/callback` as the OAuth2 callback URL
79116

80117
The other variables can be hard-coded:
81118

82-
* `successCallback` -- Specifies the URL that the user agent will be redirected to after a _successful_ authentication flow. Defaults to `"/"`.
83-
* `errorCallback` -- Specifies the URL that the user agent will be redirected to after an _unsuccessful_ authentication flow. If omitted, the user agent will receive a 401 response without redirect.
84-
* `endpointPrefix` -- Specifies the partial URL that is located between the baseUrl and the
119+
- `successCallback` -- Specifies the URL that the user agent will be redirected to after a _successful_ authentication flow. Defaults to `"/"`.
120+
- `errorCallback` -- Specifies the URL that the user agent will be redirected to after an _unsuccessful_ authentication flow. If omitted, the user agent will receive a 401 response without redirect.
121+
- `endpointPrefix` -- Specifies the partial URL that is located between the baseUrl and the
85122
authentication API endpoints.
86123

87-
For example, the following two properties
88-
- `baseUrl: "https://my-app.my-domain.com/"`
89-
- `endpointPrefix: "api/authenticate"`
124+
For example, the following two properties
125+
126+
- `baseUrl: "https://my-app.my-domain.com/"`
127+
- `endpointPrefix: "api/authenticate"`
128+
129+
will result in the API endpoints
90130

91-
will result in the API endpoints
92-
- `https://my-app.my-domain.com/api/authenticate/storyblok` for initiating the authentication flow
93-
- `https://my-app.my-domain.com/api/authenticate/storyblok/callback` as the OAuth2 callback URL
131+
- `https://my-app.my-domain.com/api/authenticate/storyblok` for initiating the authentication flow
132+
- `https://my-app.my-domain.com/api/authenticate/storyblok/callback` as the OAuth2 callback URL
94133

95134
### Create an API route
96135

@@ -110,8 +149,8 @@ Sign in a user by redirecting to the api route: `/api/connect/storyblok`
110149

111150
This will initiate the oauth flow and redirect the user to the url specified in the `successCallback` URL. The following query parameters will be appended to the `successCallback` URL:
112151

113-
* `userId`
114-
* `spaceId`
152+
- `userId`
153+
- `spaceId`
115154

116155
### Retrieve the session
117156

@@ -123,24 +162,18 @@ import { sessionCookieStore } from '@storyblok/app-extension-auth'
123162
const sessionStore = sessionCookieStore(params)(context)
124163
const appSession = await sessionStore.get(query)
125164

126-
if(appSession === undefined){
127-
// The user is not authenticated
128-
// redirect to /api/connect/storyblok
165+
if (appSession === undefined) {
166+
// The user is not authenticated
167+
// redirect to /api/connect/storyblok
129168
}
130169
```
131170

132171
### Use the session
133172

134-
The `AppSession` object contain user information for personalized content, and an access token to the Storyblok management API.
173+
The `AppSession` object contain user information for personalized content, and an access token to the Storyblok management API.
135174

136175
```typescript
137-
const {
138-
userId,
139-
spaceId,
140-
region,
141-
roles,
142-
accessToken
143-
} = appSession
176+
const { userId, spaceId, region, roles, accessToken } = appSession
144177
```
145178

146179
### Routing
@@ -164,7 +197,7 @@ const appSession = await sessionStore.get(query)
164197
When you redirect the user agent to a new page within your application, you need to _append the `spaceId` and `userId` query parameters_. Only if you do this can you retrieve the session from the `sessionCookieStore` from the other route.
165198

166199
```typescript
167-
const href = `/my/other/page?spaceId=${spaceId}&userId=${userId}`
200+
const href = `/my/other/page?spaceId=${spaceId}&userId=${userId}`
168201
```
169202

170203
## Routing for various frameworks
@@ -181,18 +214,15 @@ export default authHandler(params)
181214

182215
### Express
183216

184-
In ExpressJs, create a route
217+
In ExpressJs, create a route
185218

186219
```typescript
187220
import { authHandler } from '@storyblok/app-extension-auth'
188221

189-
app.all(
190-
'/api/connect/*',
191-
authHandler(params)
192-
)
222+
app.all('/api/connect/*', authHandler(params))
193223
```
194224

195225
## Useful Resources
196226

197-
* [Authentication Oauth2 flow](https://www.storyblok.com/docs/plugins/authentication-apps)
198-
* [Custom Applications](https://www.storyblok.com/docs/plugins/custom-application)
227+
- [Authentication Oauth2 flow](https://www.storyblok.com/docs/plugins/authentication-apps)
228+
- [Custom Applications](https://www.storyblok.com/docs/plugins/custom-application)

0 commit comments

Comments
 (0)