{"version":3,"file":"createAuthRouteHandlersFactory.mjs","sources":["../../../src/auth/createAuthRouteHandlersFactory.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { AmplifyServerContextError, assertOAuthConfig, assertTokenProviderConfig, } from 'aws-amplify/adapter-core/internals';\nimport { isAuthRoutesHandlersContext, isNextApiRequest, isNextApiResponse, isNextRequest, isValidOrigin, } from './utils';\nimport { handleAuthApiRouteRequestForAppRouter } from './handleAuthApiRouteRequestForAppRouter';\nimport { handleAuthApiRouteRequestForPagesRouter } from './handleAuthApiRouteRequestForPagesRouter';\nexport const createAuthRouteHandlersFactory = ({ config: resourcesConfig, amplifyAppOrigin, runWithAmplifyServerContext, globalSettings, }) => {\n    const handleRequest = async ({ request, contextOrResponse, handlerInput, userPoolClientId, oAuthConfig, setCookieOptions, origin, }) => {\n        if (isNextApiRequest(request) && isNextApiResponse(contextOrResponse)) {\n            // In pages router the response is sent via calling `response.end()` or\n            // `response.send()`. The response is not returned from the handler.\n            // To ensure these two methods are called before the handler returns,\n            // we use `await` here.\n            await handleAuthApiRouteRequestForPagesRouter({\n                request,\n                response: contextOrResponse,\n                handlerInput,\n                userPoolClientId,\n                oAuthConfig,\n                setCookieOptions,\n                origin,\n                runWithAmplifyServerContext,\n            });\n            // In the Pages Router, the final response is handled by contextOrResponse\n            return;\n        }\n        if (isNextRequest(request) &&\n            isAuthRoutesHandlersContext(contextOrResponse)) {\n            // In the App Router, the final response is constructed and returned\n            return handleAuthApiRouteRequestForAppRouter({\n                request,\n                handlerContext: contextOrResponse,\n                handlerInput,\n                userPoolClientId,\n                oAuthConfig,\n                setCookieOptions,\n                origin,\n                runWithAmplifyServerContext,\n            });\n        }\n        // this should not be happening\n        throw new Error('Invalid request and context/response combination. The request cannot be handled.');\n    };\n    return (createAuthRoutesHandlersInput = {}) => {\n        // origin validation should happen when createAuthRouteHandlers is being called to create\n        // Auth API routes.\n        if (!amplifyAppOrigin) {\n            throw new AmplifyServerContextError({\n                message: 'Could not find the AMPLIFY_APP_ORIGIN environment variable.',\n                recoverySuggestion: 'Add the AMPLIFY_APP_ORIGIN environment variable to the `.env` file of your Next.js project.',\n            });\n        }\n        if (!isValidOrigin(amplifyAppOrigin)) {\n            throw new AmplifyServerContextError({\n                message: 'AMPLIFY_APP_ORIGIN environment variable contains an invalid origin string.',\n                recoverySuggestion: 'Ensure the AMPLIFY_APP_ORIGIN environment variable is a valid origin string.',\n            });\n        }\n        // OAuth config validation should happen when createAuthRouteHandlers is being called to create\n        // Auth API routes.\n        assertTokenProviderConfig(resourcesConfig.Auth?.Cognito);\n        assertOAuthConfig(resourcesConfig.Auth.Cognito);\n        const { userPoolClientId } = resourcesConfig.Auth.Cognito;\n        const { oauth: oAuthConfig } = resourcesConfig.Auth.Cognito.loginWith;\n        const setCookieOptions = globalSettings.getRuntimeOptions().cookies ?? {};\n        // The call-site of this returned function is the Next.js API route file\n        return (request, contextOrResponse) => handleRequest({\n            request,\n            contextOrResponse,\n            handlerInput: createAuthRoutesHandlersInput,\n            userPoolClientId,\n            oAuthConfig,\n            setCookieOptions,\n            origin: amplifyAppOrigin,\n        });\n    };\n};\n"],"names":[],"mappings":";;;;;;;;AAAA;AACA;AAKY,MAAC,8BAA8B,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,cAAc,GAAG,KAAK;AAC/I,IAAI,MAAM,aAAa,GAAG,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,gBAAgB,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,GAAG,KAAK;AAC5I,QAAQ,IAAI,gBAAgB,CAAC,OAAO,CAAC,IAAI,iBAAiB,CAAC,iBAAiB,CAAC,EAAE;AAC/E;AACA;AACA;AACA;AACA,YAAY,MAAM,uCAAuC,CAAC;AAC1D,gBAAgB,OAAO;AACvB,gBAAgB,QAAQ,EAAE,iBAAiB;A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