hey!
diff --git a/story-starter/starters/nuxt/server/api/connect/[...slugs].ts b/story-starter/starters/nuxt/server/api/connect/[...slugs].ts new file mode 100644 index 0000000..2758e8e --- /dev/null +++ b/story-starter/starters/nuxt/server/api/connect/[...slugs].ts @@ -0,0 +1,6 @@ +import { authHandler } from '@storyblok/app-extension-auth'; +import { authHandlerParams } from '~/server/utils/auth'; + +export default defineEventHandler((event) => + authHandler(authHandlerParams)(event.node.req, event.node.res) +); diff --git a/story-starter/starters/nuxt/server/middleware/01.handle-401.global.ts b/story-starter/starters/nuxt/server/middleware/01.handle-401.global.ts new file mode 100644 index 0000000..5ab7132 --- /dev/null +++ b/story-starter/starters/nuxt/server/middleware/01.handle-401.global.ts @@ -0,0 +1,13 @@ +export default defineEventHandler(async (event) => { + if ( + event.path === '/401' && + event.headers.get('Referer') === 'https://app.storyblok.com/' && + (getCookie(event, 'sb.auth') ?? '').length > 0 + ) { + return await sendRedirect( + event, + 'https://app.storyblok.com/oauth/app_redirect', + 302 + ); + } +}); diff --git a/story-starter/starters/nuxt/server/middleware/02.auth.global.ts b/story-starter/starters/nuxt/server/middleware/02.auth.global.ts new file mode 100644 index 0000000..a09cfee --- /dev/null +++ b/story-starter/starters/nuxt/server/middleware/02.auth.global.ts @@ -0,0 +1,30 @@ +import { + isAppSessionQuery, + sessionCookieStore, +} from '@storyblok/app-extension-auth'; +import { authHandlerParams } from '../utils/auth'; +import { OAUTH_FLOW_URL, ENDPOINT_PREFIX } from '~/shared/auth'; + +export default defineEventHandler(async (event) => { + if (event.path.startsWith(ENDPOINT_PREFIX)) { + return; + } + + const query = getQuery(event); + + if (!isAppSessionQuery(query)) { + return await sendRedirect(event, OAUTH_FLOW_URL, 302); + } + + const sessionStore = sessionCookieStore(authHandlerParams)({ + req: event.node.req, + res: event.node.res, + }); + + const appSession = await sessionStore.get(query); + if (!appSession) { + return await sendRedirect(event, OAUTH_FLOW_URL, 302); + } + + // const { accessToken, region, spaceId } = appSession; +}); diff --git a/story-starter/starters/nuxt/server/utils/auth.ts b/story-starter/starters/nuxt/server/utils/auth.ts new file mode 100644 index 0000000..0ff8836 --- /dev/null +++ b/story-starter/starters/nuxt/server/utils/auth.ts @@ -0,0 +1,12 @@ +import { type AuthHandlerParams } from '@storyblok/app-extension-auth'; +import { env } from '~/server/utils/env'; +import { ENDPOINT_PREFIX } from '~/shared/auth'; + +export const authHandlerParams: AuthHandlerParams = { + clientId: env('CLIENT_ID'), + clientSecret: env('CLIENT_SECRET'), + baseUrl: env('BASE_URL'), + successCallback: '/', + errorCallback: '/401', + endpointPrefix: ENDPOINT_PREFIX, +}; diff --git a/story-starter/starters/nuxt/server/utils/env.ts b/story-starter/starters/nuxt/server/utils/env.ts new file mode 100644 index 0000000..55345c3 --- /dev/null +++ b/story-starter/starters/nuxt/server/utils/env.ts @@ -0,0 +1,10 @@ +export const env = (key: string) => { + const value = process.env[key] ?? ''; + if (typeof value !== 'string' || value.length === 0) { + throw new Error( + `[Error] Missing required environment variable: \`${key}\`` + ); + } + + return value; +}; diff --git a/story-starter/starters/nuxt/shared/auth.ts b/story-starter/starters/nuxt/shared/auth.ts new file mode 100644 index 0000000..6ab9fe7 --- /dev/null +++ b/story-starter/starters/nuxt/shared/auth.ts @@ -0,0 +1,2 @@ +export const ENDPOINT_PREFIX = '/api/connect'; +export const OAUTH_FLOW_URL = `${ENDPOINT_PREFIX}/storyblok`;