Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@storyblok/app-extension-auth

Package Overview
Dependencies
Maintainers
7
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@storyblok/app-extension-auth

A typed JavaScript library for handling authentication with Storyblok apps.

  • 1.0.0
  • alpha
  • Source
  • npm
  • Socket score

Version published
Maintainers
7
Created
Source

@storyblok/app-extension-auth

A JavaScript library for managing authentication for Storyblok apps.

Getting Started

See our starters:

  • Next.js

Install the app

Install with npm:

npm install --save-exact @storyblok/app-extension-auth

or with Yarn:

yarn add --exact @storyblok/app-extension-auth

Note: the @storyblok/app-extension-auth is currently in alpha, and is prone to changes. Therefore, save the exact version to your package.json with the commands above.

Set a URL

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.

To open a secure tunnel with ngrok, run:

ngrok http 3000

Set up an App in Storyblok's Partner Portal

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

  • URL to your app: the index page of your app. For example, https://my-app.com/.

  • OAuth2 callback URL: the api endpoint that will initiate the OAuth flow.

    • Calculated as: {baseUrl}/{endpointPrefix}/storyblok/callback
    • Example value: https://my-app.com/api/connect/storyblok/callback

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

Define constants

In your source code, create the following object (you will need it later):

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',  
}

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

  • clientId -- The client ID is a public identifier for your apps. Find the Client ID in the app settings on Storyblok.

  • 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.

    Load it into the application as an environmental variable. It must be kept confidential.

  • baseUrl -- The base URL specifies the base URL to use for all relative authentication API endpoints created by authHandler(). The base URL must be absolute and secure with https.

    For example, the base URL https://my-app.my-domain.com/ will create the following api endpoints:

    • https://my-app.my-domain.com/storyblok for initiating the authentication flow
    • https://my-app.my-domain.com/storyblok/callback as the OAuth2 callback URL

The other variables can be hard-coded:

  • successCallback -- Specifies the URL that the user agent will be redirected to after a successful authentication flow. Defaults to "/".

  • 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.

  • endpointPrefix -- Specifies the partial URL that is located between the baseUrl and the authentication API endpoints.

    For example, the following two properties

    • baseUrl: "https://app.com"
    • endpointPrefix: "api/authenticate"

    will result in the API endpoints

    • https://my-app.my-domain.com/api/authenticate/storyblok for initiating the authentication flow
    • https://my-app.my-domain.com/api/authenticate/storyblok/callback as the OAuth2 callback URL

Create an API route

In NodeJS, create a dynamic route that handles the incoming requests with authHandler(). See [Framework examples](#Routing for various frameworks).

For example, in Next.js, create a file pages/api/connect/[...slugs].ts:

import { authHandler } from '@storyblok/app-extension-auth'

export default authHandler(params)

Sign in

Sign in a user by redirecting to the api route: /api/connect/storyblok

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:

  • userId
  • spaceId

Retrieve the session

Now, use these two query parameters to retrieve the session object:

import { sessionCookieStore } from '@storyblok/app-extension-auth'

const sessionStore = sessionCookieStore(params)(context)
const appSession = await sessionStore.get(query)

if(appSession === undefined){
    // The user is not authenticated
    //  redirect to /api/connect/storyblok
}

Use the session

The AppSession object contain user information for personalized content, and an access token to the Storyblok management API.

const {
  userId,
  spaceId,
  region,
  roles,
  accessToken
} = appSession

Routing

Storyblok apps are embedded within Storyblok via iframes. When a page is requested, the server must get to know

  1. spaceId: the space the page is being embedded within
  2. userId: the user who loaded the page

These two values needs to be encoded within the page request.

If these two values cannot be retrieved, you need to initiate the OAuth flow by redirecting the user agent to /api/connect/storyblok. After a successful authentication, the spaceId and userId will be added as query parameters to the successCallback value. Now, it should be possible to retrieve the session like so

import { sessionCookieStore } from '@storyblok/app-extension-auth'

const sessionStore = sessionCookieStore(params)(context)
const appSession = await sessionStore.get(query)

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.

const href =  `/my/other/page?spaceId=${spaceId}&userId=${userId}`

Routing for various frameworks

Next.js

In Next.js, create a file pages/api/connect/[...slugs].ts

import { authHandler } from '@storyblok/app-extension-auth'

export default authHandler(params)

Express

In ExpressJs, create a route

import { authHandler } from '@storyblok/app-extension-auth'

app.all(
    '/api/connect/*', 
    authHandler(params)
)

Useful Resources

FAQs

Package last updated on 31 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc