Skip to content

Commit

Permalink
use origin
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed May 10, 2024
1 parent 1dbe6e0 commit 30d0cd2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
Expand Up @@ -18,6 +18,7 @@ import { completeOAuthFlow } from './utils/completeOAuthFlow';

export const createOAuthRouteHandlerFactory: CreateOAuthRouteHandlerFactory = ({
config: resourcesConfig,
origin,
setAuthCookieOptions,
}): CreateOAuthRouteHandler => {
const handleRequest = async (
Expand All @@ -28,6 +29,7 @@ export const createOAuthRouteHandlerFactory: CreateOAuthRouteHandlerFactory = ({
onError,
}: CreateOAuthRouteHandlerInput,
): Promise<Response | void> => {
if (!origin) throw new Error('Origin is not provided');
assertTokenProviderConfig(resourcesConfig.Auth?.Cognito);
assertOAuthConfig(resourcesConfig.Auth.Cognito);

Expand All @@ -37,6 +39,7 @@ export const createOAuthRouteHandlerFactory: CreateOAuthRouteHandlerFactory = ({
// when request url has `init` query param - initiate oauth flow
if (searchParams.has('init')) {
return initOAuthFlow({
origin,
setAuthCookieOptions,
request,
customState,
Expand All @@ -47,6 +50,7 @@ export const createOAuthRouteHandlerFactory: CreateOAuthRouteHandlerFactory = ({

if (searchParams.has('code') && searchParams.has('state')) {
return completeOAuthFlow({
origin,
request,
redirectOnComplete: redirectOnAuthComplete,
setAuthCookieOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-nextjs/src/oauth/types.ts
Expand Up @@ -34,6 +34,7 @@ export type CreateOAuthRouteHandler = (

interface CreateOAuthRouteHandlerFactoryInput {
config: ResourcesConfig;
origin?: string;
setAuthCookieOptions?: NextServer.SetCookieOptions;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/adapter-nextjs/src/oauth/utils/completeOAuthFlow.ts
Expand Up @@ -23,12 +23,14 @@ import { createCookieStorageAdapterFromNextServerContext } from '../../utils/cre
import { getRedirectUrl } from './getRedirectUrl';

export const completeOAuthFlow = async ({
origin,
request,
redirectOnComplete,
cognitoUserPoolConfig,
oAuthConfig,
setAuthCookieOptions,
}: {
origin: string;
request: NextRequest;
customState: string | undefined;
redirectOnComplete: string;
Expand Down Expand Up @@ -72,7 +74,7 @@ export const completeOAuthFlow = async ({
code,
client_id: cognitoUserPoolConfig.userPoolClientId,
// TODO(Hui): request.nextUrl.origin should be generic and not use Next specifics
redirect_uri: getRedirectUrl(request.nextUrl.origin, oAuthConfig),
redirect_uri: getRedirectUrl(origin, oAuthConfig),
...(codeVerifier ? { code_verifier: codeVerifier } : {}),
};

Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-nextjs/src/oauth/utils/initOAuthFlow.ts
Expand Up @@ -25,6 +25,7 @@ export const initOAuthFlow = async ({
oAuthConfig,
setAuthCookieOptions,
}: {
origin: string;
request: NextRequest;
customState: string | undefined;
cognitoUserPoolConfig: CognitoUserPoolConfig;
Expand All @@ -41,7 +42,7 @@ export const initOAuthFlow = async ({
const scope = oAuthConfig.scopes.join(' ');

const redirectUrlSearchParams = new URLSearchParams({
redirect_uri: getRedirectUrl(request.nextUrl.origin, oAuthConfig),
redirect_uri: getRedirectUrl(origin, oAuthConfig),
response_type: oAuthConfig.responseType,
client_id: cognitoUserPoolConfig.userPoolClientId!,
identity_provider: provider,
Expand Down

0 comments on commit 30d0cd2

Please sign in to comment.