From b7cf3327a9fa30135f2c4f4991c56d7de1669da1 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Wed, 3 Jul 2024 16:39:46 +0200 Subject: [PATCH] apply init_oauth merge --- src/storyblok-auth-api/auth-handler.ts | 14 ++------------ .../handle-requests/handleAny/handleAnyRequest.ts | 3 +-- .../handleSignIn/handleSignInRequest.ts | 13 ++++++++++++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/storyblok-auth-api/auth-handler.ts b/src/storyblok-auth-api/auth-handler.ts index 33c6512..bb90c7c 100644 --- a/src/storyblok-auth-api/auth-handler.ts +++ b/src/storyblok-auth-api/auth-handler.ts @@ -4,8 +4,6 @@ import { handleAnyRequest } from './handle-requests' import { reconcileNodeResponse } from './reconcileNodeResponse' import { cookieAdapter } from '../session-adapters/cookieAdapter' import { createInternalAdapter } from '../session-adapters/internalAdapter' -import { getQueryParams } from '../utils/query-params/get-query-params' -import { sessionIdentifier } from '../session/sessionIdentifier' /** * Auth handler for Node.js @@ -21,24 +19,16 @@ export const authHandler = ( return } - const internalAdapter = createInternalAdapter({ + const adapter = createInternalAdapter({ req, res, adapter: cookieAdapter, }) - const query = getQueryParams(url) - const isInitRequest = query.get('init_oauth') - - // NOTE: This is a workaround to remove a stale cookie in case the user has reinstalled the plugin. - if (isInitRequest === 'true') { - await internalAdapter.removeItem(sessionIdentifier(params)) - } - const responseElement = await handleAnyRequest({ params, url, - adapter: internalAdapter, + adapter, }) await reconcileNodeResponse({ diff --git a/src/storyblok-auth-api/handle-requests/handleAny/handleAnyRequest.ts b/src/storyblok-auth-api/handle-requests/handleAny/handleAnyRequest.ts index 58dc12d..66325dc 100644 --- a/src/storyblok-auth-api/handle-requests/handleAny/handleAnyRequest.ts +++ b/src/storyblok-auth-api/handle-requests/handleAny/handleAnyRequest.ts @@ -29,10 +29,9 @@ export const handleAnyRequest: HandleAuthRequest<{ } const lastSlug = getLastSlug(url) - switch (lastSlug) { case signinEndpoint: - return handleSignInRequest({ params, adapter }) + return handleSignInRequest({ url, params, adapter }) case callbackEndpoint: return handleCallbackRequest({ url, params, adapter }) default: diff --git a/src/storyblok-auth-api/handle-requests/handleSignIn/handleSignInRequest.ts b/src/storyblok-auth-api/handle-requests/handleSignIn/handleSignInRequest.ts index 042a38f..11e94d3 100644 --- a/src/storyblok-auth-api/handle-requests/handleSignIn/handleSignInRequest.ts +++ b/src/storyblok-auth-api/handle-requests/handleSignIn/handleSignInRequest.ts @@ -4,11 +4,13 @@ import { redirectUri } from '../redirectUri' import { AuthHandlerParams } from '../../AuthHandlerParams' import { HandleAuthRequest } from '../HandleAuthRequest' import { InternalAdapter } from '../../../session-adapters/internalAdapter' +import { getQueryParams } from '../../../utils/query-params/get-query-params' export const handleSignInRequest: HandleAuthRequest<{ + url: string params: AuthHandlerParams adapter: InternalAdapter -}> = async ({ params, adapter }) => { +}> = async ({ url, params, adapter }) => { const code_verifier = generators.codeVerifier() const state = generators.state() const code_challenge = generators.codeChallenge(code_verifier) @@ -24,6 +26,15 @@ export const handleSignInRequest: HandleAuthRequest<{ redirect_uri: redirectUri(params), }) + const query = getQueryParams(url) + const isInitRequest = query.get('init_oauth') + + // NOTE: This is a workaround to remove a stale cookie in case the user has reinstalled the plugin. + if (isInitRequest === 'true') { + // FIX: spaceId and userId don't exist here. + // await adapter.removeSession({ spaceId, userId }) + } + await adapter.setCallbackData({ returnTo: params?.successCallback ?? '/', // TODO read from request query params, then either use the successCallback as fallback, or remove the entirely codeVerifier: code_verifier,