Skip to content

Commit

Permalink
apply init_oauth merge
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Jul 3, 2024
1 parent 5f3db66 commit b7cf332
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 2 additions & 12 deletions src/storyblok-auth-api/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit b7cf332

Please sign in to comment.