@storyblok/app-extension-auth
@storyblok/app-extension-auth is a JavaScript library for managing authentication in Storyblok apps.
Installation
Section titled “Installation”npm install @storyblok/app-extension-auth@latestEnvironment variables
Section titled “Environment variables”APP_CLIENT_ID=your-app-client-idAPP_CLIENT_SECRET=your-app-client-secretAPP_URL=https://your-app.com/APP_CLIENT_ID: The public identifier for your app. Available in your Storyblok app settings.APP_CLIENT_SECRET: The secret shared between your app and Storyblok. Keep this private.APP_URL: Base URL for your app. Must be absolute and use HTTPS.
For example, using APP_URL=https://your-app.com/ creates these endpoints:
https://your-app.com/storyblokfor initiating authenticationhttps://your-app.com/storyblok/callbackfor the OAuth2 callback
Configure authentication
Section titled “Configure authentication”Define the parameters once and reuse them across your app:
import { AuthHandlerParams } from '@storyblok/app-extension-auth'
export const params: AuthHandlerParams = { clientId: process.env.APP_CLIENT_ID, clientSecret: process.env.APP_CLIENT_SECRET, baseUrl: process.env.APP_URL, successCallback: '/', errorCallback: '/401', endpointPrefix: '/api/connect',}Optional parameters:
successCallback: Redirect URL after successful login (default/).errorCallback: Redirect URL after failed login (default none, returns401).endpointPrefix: Path prefix for authentication routes.
Example
Section titled “Example”baseUrl: "https://your-app.com/";endpointPrefix: "api/authenticate";This creates the following endpoints:
https://your-app.com/api/authenticate/storyblokhttps://your-app.com/api/authenticate/storyblok/callback
Create routes
Section titled “Create routes”Use authHandler to process authentication requests.
Examples
Section titled “Examples”Next.js
Section titled “Next.js”import { authHandler } from "@storyblok/app-extension-auth";import { params } from "@/config/auth";
export default authHandler(params);Express
Section titled “Express”import { authHandler } from "@storyblok/app-extension-auth";import { params } from "@/config/auth";
app.all("/api/connect/*", authHandler(params));Sign-in flow
Section titled “Sign-in flow”Redirect users to /api/connect/storyblok to start authentication.
After login, users are redirected to the successCallback URL with these query parameters:
userIdspaceId
Session store
Section titled “Session store”Use getSessionStore to manage sessions:
import { getSessionStore } from "@storyblok/app-extension-auth";
const sessionStore = getSessionStore(params)({ req, res });//req: http.IncomingMessage;//res: http.ServerResponse;Available methods:
get: Fetch a session byuserIdandspaceId.getAll: Fetch all sessions for a user.put: Store a session. ReturnsPromise<boolean>.remove: Delete a session. ReturnsPromise<boolean>
Retrieve a session
Section titled “Retrieve a session”import { getSessionStore, inferSessionQuery } from "@storyblok/app-extension-auth";const sessionStore = getSessionStore(params)({ req, res });const appSessionQuery = inferSessionQuery(req);const appSession = await sessionStore.get(appSessionQuery);if (!appSession) { // Not authenticated // redirect to /api/connect/storyblok}Use the session
Section titled “Use the session”The AppSession object contains user data and an access token for the Storyblok Management API:
const { userId, spaceId, region, roles, accessToken } = appSession;Routing considerations
Section titled “Routing considerations”Storyblok apps run inside iframes.
Always pass spaceId and userId as query parameters when linking between pages, so the session can be retrieved:
const href = `/my/other/page?spaceId=${spaceId}&userId=${userId}`;Local development
Section titled “Local development”To test against a local Storyblok backend, add storyblokApiBaseUrl:
const params: AuthHandlerParams = { // ... storyblokApiBaseUrl: 'http://localhost:1234',}Expose your local server with a tool like ngrok:
ngrok http 3000Further resources
Section titled “Further resources”Get in touch with the Storyblok community