Skip to content

Commit

Permalink
chore: adding warnings regarding using custom endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Oct 29, 2024
1 parent 5155098 commit 91e03b0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/core/__tests__/singleton/Auth/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ConsoleLogger } from '../../../src/Logger';
import { AuthClass } from '../../../src/singleton/Auth';

jest.mock('../../../src/Logger', () => {
const warn = jest.fn();

return {
ConsoleLogger: jest.fn(() => ({
warn,
})),
};
});

describe('Auth', () => {
const auth = new AuthClass();
const logger = new ConsoleLogger('Auth');
const mockedWarn = logger.warn as jest.Mock;

describe('configure', () => {
const mockConfig = {
userPoolClientId: 'userPoolClientId',
userPoolId: 'userPoolId',
};

it('prints warning when use custom endpoint for Cognito User Pool', () => {
auth.configure({
Cognito: {
...mockConfig,
userPoolEndpoint: 'https://custom-endpoint.com',
},
});

expect(mockedWarn).toHaveBeenCalledWith(
expect.stringContaining('Amazon Cognito User Pool'),
);
});

it('prints warning when use custom endpoint for Cognito Identity Pool', () => {
auth.configure({
Cognito: {
...mockConfig,
identityPoolId: 'identityPoolId',
identityPoolEndpoint: 'https://custom-endpoint.com',
},
});

expect(mockedWarn).toHaveBeenCalledWith(
expect.stringContaining('Amazon Cognito Identity Pool'),
);
});
});
});
17 changes: 17 additions & 0 deletions packages/core/src/singleton/Auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ConsoleLogger } from '../../Logger';

import {
AuthConfig,
AuthSession,
Expand All @@ -9,6 +11,8 @@ import {
LibraryAuthOptions,
} from './types';

const logger = new ConsoleLogger('Auth');

export function isTokenExpired({
expiresAt,
clockDrift,
Expand Down Expand Up @@ -41,6 +45,16 @@ export class AuthClass {
): void {
this.authConfig = authResourcesConfig;
this.authOptions = authOptions;

if (authResourcesConfig.Cognito.userPoolEndpoint) {
logger.warn(getCustomEndpointWarningMessage('Amazon Cognito User Pool'));
}

if (authResourcesConfig.Cognito.identityPoolEndpoint) {
logger.warn(
getCustomEndpointWarningMessage('Amazon Cognito Identity Pool'),
);
}
}

/**
Expand Down Expand Up @@ -106,3 +120,6 @@ export class AuthClass {
);
}
}

const getCustomEndpointWarningMessage = (target: string): string =>
`You are using a custom Amazon ${target} endpoint, ensure the endpoint is correct.`;
6 changes: 6 additions & 0 deletions packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ export interface AuthIdentityPoolConfig {

export interface CognitoIdentityPoolConfig {
identityPoolId: string;
/**
* Use this field to specify a custom endpoint for the Amazon Cognito identity pool. Ensure this endpoint is correct and valid.
*/
identityPoolEndpoint?: string;
allowGuestAccess?: boolean;
}
Expand All @@ -152,6 +155,9 @@ export type CognitoUserPoolConfigMfaStatus = 'on' | 'off' | 'optional';
export interface CognitoUserPoolConfig {
userPoolClientId: string;
userPoolId: string;
/**
* Use this field to specify a custom endpoint for the Amazon Cognito user pool. Ensure this endpoint is correct and valid.
*/
userPoolEndpoint?: string;
signUpVerificationMethod?: 'code' | 'link';
loginWith?: {
Expand Down

0 comments on commit 91e03b0

Please sign in to comment.